开机自动执行
开机自动执行指定程式
将开机需要执行的指定程式写入 /etc/rc.local
即可
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# 开机自动启动
bash /home/ubuntu/scripts/boot.sh > /dev/null 2>&1
# 自动部署程式
bash /home/ubuntu/scripts/deploy.sh > /dev/null 2>&1
输入下列指令可以检查目前这个服务执行的状态
systemctl status rc.local.service
$ systemctl status rc.local.service
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
Drop-In: /lib/systemd/system/rc-local.service.d
└─debian.conf
Active: active (exited) since Wed 2019-09-25 15:52:07 CST; 33min ago
Docs: man:systemd-rc-local-generator(8)
Process: 793 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
Sep 25 15:54:33 ip-172-31-25-154 rc.local[793]: Deploy Done
使用 crontab 重新开机后自动执行
crontab -e
# 开机自动启动
@reboot bash /home/ubuntu/scripts/boot.sh > /dev/null 2>&1
# 自动部署程式
@reboot bash /home/ubuntu/scripts/deploy.sh > /dev/null 2>&1
参考资料
- boot - Run bash script on startup - Raspberry Pi Stack Exchange
- Schedule Tasks on Linux Using Crontab | kvz.io