Ubuntu 20.04 开机执行自定义脚本
家里的服务器重新安装了系统,有些shell脚本需要开机启动,记录一下方法,免得下次又忘了。
0. 请使用root权限执行以下操作
1. 创建rc-local.service文件
vi /etc/systemd/system/rc-local.service
然后复制service文件配置,不用做任何修改。
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
2. 创建rc.local文件
vi /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.
#start script
#end script
echo "added sucessfully!" > /tmp/added_script.log
exit 0
在“#start script”和“#end script”中编辑自己想要执行的shell命令即可。
3. 赋予可自行权限
chmod +x /etc/rc.local
4. 设置开机启动
systemctl enable rc-local
4. 其他操作
后续就可以使用systemctl进行管理。
systemctl start rc-local.service
systemctl stop rc-local.service
systemctl status rc-local.service
via.https://zhangyaoyu.com/archives/399/
参考:https://blog.csdn.net/qq_36328643/article/details/89359724