2016-08-05 503 views
3

我有一個簡單的腳本「start.sh」,我想在每次啓動ubuntu 16.04時使它自動啓動。使用systemd在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 

更新

syslog中,我得到了這一點。 /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

它能夠ŧ o重啓後啓動服務,但1-2分鐘後,服務消失。見下文。

[email protected]:~$ 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 
[email protected]:~$ cd misc/ 
[email protected]:~/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 
[email protected]:~/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 
[email protected]:~/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'. 
+1

你用'systemctl enable'啓用了服務嗎? –

+0

通常在Ubuntu – Matt

+0

上有'nodejs',當你有systemd – Matt

回答

1

下systemd永遠使用有點像用永遠永遠運行。

沒有永遠,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,並且還有一個匹配的系統PIDFile=

+0

看到我的更新1 – kenpeter