我按照本指南安装了 no-ip 动态 ddns: https://www.noip.com/support/knowledgebase/installing-the-linux-dynamic-update-client/
我让服务运行
sudo /usr/local/bin/noip2
但是我希望服务在启动时启动,我尝试将以下脚本添加到 /etc/init.d/noip2.sh
#######################################################
#! /bin/sh
# . /etc/rc.d/init.d/functions # uncomment/modify for your killproc
case "$1" in
start)
echo "Starting noip2."
/usr/local/bin/noip2
;;
stop)
echo -n "Shutting down noip2."
killproc -TERM /usr/local/bin/noip2
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
#######################################################
其次是:
sudo chmod +x /etc/init.d/noip2.sh
sudo update-rc.d noip2.sh defaults
现在我应该能够使用以下命令启动该服务
sudo service noip2 start
但我没有。当我运行时,journalctl -xe
我得到以下信息:
-- Unit noip2.service has begun starting up.
Nov 03 12:36:11 media systemd[3111]: noip2.service: Failed to execute command: Exec format error
Nov 03 12:36:11 media systemd[3111]: noip2.service: Failed at step EXEC spawning /etc/init.d/noip2.sh: Exec format error
-- Subject: Process /etc/init.d/noip2.sh could not be executed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- The process /etc/init.d/noip2.sh could not be executed and failed.
--
-- The error number returned by this process is 8.
Nov 03 12:36:11 media systemd[1]: noip2.service: Control process exited, code=exited status=203
Nov 03 12:36:11 media systemd[1]: noip2.service: Failed with result 'exit-code'.
Nov 03 12:36:11 media systemd[1]: Failed to start noip2.service.
-- Subject: Unit noip2.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
用户 PerDuck 的更新信息:我在尝试您的解决方案时收到以下错误... :( 尝试添加
RestartSec=30
至少它现在一直在尝试,但仍然无法启动。我仍然可以使用 sudo /usr/local/bin/noip2 启动它
错误:
Nov 03 23:26:42 media systemd[1]: noip2.service: Service hold-off time over, scheduling restart.
Nov 03 23:26:42 media systemd[1]: noip2.service: Scheduled restart job, restart counter is at 5.
Nov 03 23:26:42 media systemd[1]: Stopped noip2 service.
Nov 03 23:26:42 media systemd[1]: noip2.service: Start request repeated too quickly.
Nov 03 23:26:42 media systemd[1]: noip2.service: Failed with result 'start-limit-hit'.
Nov 03 23:26:42 media systemd[1]: Failed to start noip2 service.
答案1
从 Ubuntu 15.04 开始,控制后台进程(以及更多)的标准方法是systemd
。我建议从init.d
脚本切换到systemd
单元:
/etc/systemd/system/noip2.service
创建包含以下内容的文件(并删除您的init.d
脚本):
[Unit]
Description=noip2 service
[Service]
Type=forking
ExecStart=/usr/local/bin/noip2
Restart=always
[Install]
WantedBy=default.target
然后发出
sudo systemctl daemon-reload
了解systemd
新的单元(systemd
缓存单元文件并且此命令重新systemd
考虑其缓存)。
现在您可以尝试启动和停止您的单元并查看其状态:
sudo systemctl status noip2
sudo systemctl start noip2
sudo systemctl status noip2
sudo systemctl stop noip2
sudo systemctl status noip2
要让设备在启动时启动,您需要使能够它:
sudo systemctl enable noip2
要在启动时禁用自动启动,您必须禁用那个单位:
sudo systemctl disable noip2
大多数时候五个命令足以控制一个单位的行为:
systemctl start $unit # starts a unit NOW
systemctl stop $unit # stops a unit NOW
systemctl status $unit # shows status
systemctl enable $unit # starts a unit at boot time (but not NOW)
systemctl disable $unit # stops autostart (but doesn't stop the unit NOW)
您还可以启用自动启动并立即启动该设备,或者禁用自动启动并立即停止它:
systemctl enable --now $unit # enable and start in one go
systemctl disable --now $unit # disable and stop in one go
更新
一些研究表明noip2
跑步是一种守护进程即当你启动它时,它会创建另一个在后台运行的进程(所谓的分叉),前台进程立即返回(退出)。这就是 init.d 脚本和 systemd 单元失败的原因:它们启动后就noip2
立即退出。因此,systemd 尝试一遍又一遍地重新启动它,但无济于事。(默认情况下,systemd 在 10 秒左右内最多重新启动进程 5 次,然后放弃并使其处于失败状态。)
告诉 systemd 该单元属于类型分叉添加行
Type=forking
就像[Service]
我在上面的代码片段中所做的那样。这告诉 systemd预计主进程立即返回,而是观察由 生成(分叉)的进程noip2
。