【问题标题】:Use systemd to start a script at ubuntu 16.04 boot在 ubuntu 16.04 启动时使用 systemd 启动脚本
【发布时间】:2016-08-06 10:57:51
【问题描述】:

我有一个简单的脚本“start.sh”,我想让它在每次 ubuntu 16.04 重新启动时自动启动。

我关注了这个帖子:https://serverfault.com/questions/773162/i-cant-get-the-right-items-to-boot-on-ubuntu-16-04-and-systemd

制作这个简单的脚本:https://github.com/kenpeter/test_run_at_boot

我把它放在这里了。

/lib/systemd/system/test_run_at_boot.service

我已经链接了

/etc/systemd/system/multi-user.target.wants/test_run_at_boot.service -> /lib/systemd/system/test_run_at_boot.service

每次我重新启动我的机器,它都没有启动。

我也尝试将实际命令附加到 test_run_at_boot.service,但仍然无法正常工作。

[Unit]
Description=test_run_at_boot

[Service]
ExecStart=forever start -a -l forever.log -o out.log -e err.log /home/ubuntu/misc/service/test_run_at_boot/test_run_at_boot.js

[Install]
WantedBy=multi-user.target

更新

在系统日志中,我得到了这个。 /usr/bin/env: ‘node’: No such file or directory 这会导致脚本在启动时失败。

Aug  5 06:45:23 xyz start.sh[1557]: /usr/bin/env: ‘node’: No such file or directory
Aug  5 06:45:23 xyz systemd[1]: Starting LSB: Record successful boot for GRUB...
Aug  5 06:45:23 xyz acpid: starting up with netlink and the input layer
Aug  5 06:45:23 xyz acpid: 1 rule loaded
Aug  5 06:45:23 xyz acpid: waiting for events: event logging is off
Aug  5 06:45:23 xyz systemd[1]: Started D-Bus System Message Bus.
Aug  5 06:45:23 xyz dbus[1590]: [system] AppArmor D-Bus mediation is enabled
Aug  5 06:45:23 xyz systemd[1]: Started Cgroup management daemon.
Aug  5 06:45:23 xyz systemd[1]: Starting Thermal Daemon Service...
Aug  5 06:45:23 xyz systemd[1]: Started FUSE filesystem for LXC.
Aug  5 06:45:23 xyz systemd[1]: Starting Restore /etc/resolv.conf if the system crashed before the ppp link was shut down...
Aug  5 06:45:23 xyz lxcfs[1624]: hierarchies: 0: memory
Aug  5 06:45:23 xyz lxcfs[1624]:  1: blkio
Aug  5 06:45:23 xyz lxcfs[1624]:  2: hugetlb
Aug  5 06:45:23 xyz lxcfs[1624]:  3: cpuset
Aug  5 06:45:23 xyz lxcfs[1624]:  4: freezer
Aug  5 06:45:23 xyz lxcfs[1624]:  5: net_cls,net_prio
Aug  5 06:45:23 xyz lxcfs[1624]:  6: pids
Aug  5 06:45:23 xyz lxcfs[1624]:  7: devices
Aug  5 06:45:23 xyz lxcfs[1624]:  8: cpu,cpuacct
Aug  5 06:45:23 xyz lxcfs[1624]:  9: perf_event
Aug  5 06:45:23 xyz lxcfs[1624]:  10: name=systemd
Aug  5 06:45:23 xyz systemd[1]: Starting System Logging Service...
Aug  5 06:45:23 xyz systemd[1]: Started Snappy daemon.
Aug  5 06:45:23 xyz systemd[1]: Started Regular background program processing daemon.
Aug  5 06:45:23 xyz cron[1648]: (CRON) INFO (pidfile fd = 3)
Aug  5 06:45:23 xyz systemd[1]: Starting Accounts Service...
Aug  5 06:45:23 xyz cron[1648]: (CRON) INFO (Running @reboot jobs)
Aug  5 06:45:23 xyz systemd[1]: Starting Login Service...
Aug  5 06:45:23 xyz systemd[1]: Starting LXD - container startup/shutdown...
Aug  5 06:45:23 xyz systemd[1]: Started System Logging Service.
Aug  5 06:45:23 xyz systemd[1]: test_run_at_boot.service: Main process exited, code=exited, status=127/n/a
Aug  5 06:45:23 xyz systemd[1]: test_run_at_boot.service: Unit entered failed state.
Aug  5 06:45:23 xyz systemd[1]: test_run_at_boot.service: Failed with result 'exit-code'.

更新 1

我关注这个帖子

https://patrakov.blogspot.com.au/2011/01/writing-systemd-service-files.html

我做了一些改变

https://github.com/kenpeter/test_run_at_boot/blob/master/test_run_at_boot.service

重启后可以启动服务,但1-2分钟后,服务消失。见下文。

ubuntu@xyz:~$ ps aux | grep test
root      2155  0.3  3.8 942196 38780 ?        Ssl  13:04   0:00 /home/ubuntu/.nvm/versions/node/v6.3.1/bin/node /home/ubuntu/.nvm/versions/node/v6.3.1/lib/node_modules/forever/bin/monitor /home/ubuntu/misc/service/test_run_at_boot/test_run_at_boot.js
ubuntu    3223  0.0  0.1  12944  1092 pts/0    S+   13:05   0:00 grep --color=auto test
ubuntu@xyz:~$ cd misc/
ubuntu@xyz:~/misc/service/test_run_at_boot$ ls
err.log  node_modules  out.log  package.json  start.sh  stop.sh  test_run_at_boot.js  test_run_at_boot.service
ubuntu@xyz:~/misc/service/test_run_at_boot$ ps aux | grep test
ubuntu    3240  0.0  0.0  12944  1012 pts/0    S+   13:05   0:00 grep --color=auto test
ubuntu@xyz:~/misc/service/test_run_at_boot$ 

更新 2

在系统日志中

Aug  6 01:24:22 xyz start.sh[1663]: warn:    --minUptime not set. Defaulting to: 1000ms
Aug  6 01:24:22 xyz start.sh[1663]: warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
Aug  6 01:24:22 xyz start.sh[1663]: info:    Forever processing file: /home/ubuntu/misc/service/test_run_at_boot/test_run_at_boot.js
Aug  6 01:24:22 xyz systemd[1]: test_run_at_boot.service: PID 3347 read from file /home/ubuntu/misc/service/test_run_at_boot/test_run_at_boot.pid does not exist or is a zombie.
....
....
Aug  6 01:24:55 xyz systemd[1]: Started Execute cloud user/final scripts.
Aug  6 01:24:55 xyz systemd[1]: Reached target Cloud-init target.
Aug  6 01:25:00 xyz ntpdate[2709]: adjust time server 91.189.91.157 offset 0.104466 sec
Aug  6 01:25:51 xyz systemd[1]: test_run_at_boot.service: Start operation timed out. Terminating.
Aug  6 01:25:51 xyz systemd[1]: Failed to start test_run_at_boot.
Aug  6 01:25:51 xyz systemd[1]: test_run_at_boot.service: Unit entered failed state.
Aug  6 01:25:51 xyz systemd[1]: test_run_at_boot.service: Failed with result 'timeout'.

【问题讨论】:

  • 您是否启用了systemctl enable的服务?
  • 在 Ubuntu 上通常是 nodejs
  • 当你拥有 systemd 后,你真的不需要永远
  • 这更像是unix的问题

标签: node.js ubuntu systemd forever ubuntu-16.04


【解决方案1】:

在 systemd 下使用 forever 有点像使用 forever 来永久运行。

没有永远,systemd 配置变得更简单。

[Unit]
Description=test_run_at_boot

[Service]
ExecStart=/usr/bin/nodejs /home/ubuntu/misc/service/test_run_at_boot/test_run_at_boot.js
Type=oneshot
#Restart=on-failure

[Install]
WantedBy=multi-user.target

您正在使用的测试脚本是Type=oneshot,因为脚本预计会退出,否则 systemd 将报告服务失败。一旦你有一个长期运行的服务需要管理,你可以删除 Type=oneshot 并使用 Restart=on-failure 来保持服务正常运行,就像永远一样。

来自进程的stderrstdout 被发送到systemd 日志。在 Ubuntu 上,这转到 syslog。

如果你真的需要永久的特性,那么 systemd 需要更多的设置。 Type becomes forking。您将需要设置一个 --pidFile 并拥有一个匹配的 systemd PIDFile=

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 2017-09-01
    • 2013-06-29
    • 2013-02-15
    • 1970-01-01
    相关资源
    最近更新 更多