Ubuntu设置开机启动执行脚本
1、创建rc-local.service文件
sudo vi /etc/systemd/system/rc-local.service
2、将下面代码复制进去
[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
3、创建文件rc.local
sudo vi /etc/rc.local
4、将下面代码复制进去
#!/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.
echo "如果你看到这行文字说明执行成功!" > /usr/local/mytest.log
exit 0
5、给rc.local加上权限
sudo chmod +x /etc/rc.local
6、启用服务
sudo systemctl enable rc-local
7、启动服务并检查状态
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
如果输入下面的状态,表示启动成功。
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset:
Drop-In: /lib/systemd/system/rc-local.service.d
删除mytest.log重启之后检查/usr/local/mytest.log即可判断是否启动成功。
之后修改rc.local中的脚本就可以添加自己想要的内容了。