ubuntu18.04不再使用initd管理系统,改用systemd。
ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用。
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=
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority= [Install]
WantedBy=multi-user.target
) [Unit] 区块:启动顺序与依赖关系。 ) [Service] 区块:启动行为,如何启动,启动类型。 ) [Install] 区块,定义如何安装这个配置文件,即怎样做到开机启动。
有关说明
3. 创建我们自定义的启动脚本(以后自己的脚本在这里面添加即可)
$ sudo 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.
touch /home/xxx/`date +%Y-%m-%d`.log
echo "`df`" > /home/xxx/`date +%Y-%m-%d`.log
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
exit 0
#
# 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.
touch /home/xxx/`date +%Y-%m-%d`.log
echo "`df`" > /home/xxx/`date +%Y-%m-%d`.log
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
exit 0
#!/bin/sh -e -e 参数可以让脚本执行遇到错误就不往下执行
有关说明
$ sudo chmod +x /etc/rc.local && sudo systemctl enable rc-local
Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /etc/systemd/system/rc-local.service.
启动服务,并检查服务(非必要)
$ 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
└─debian.conf
Active: active (exited) since Fri 2019-01-25 09:31:07 CST; 9s ago
Process: 17588 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS) Jan 25 09:31:07 SCHIPS systemd[1]: Starting /etc/rc.local Compatibility...
Jan 25 09:31:07 SCHIPS systemd[1]: Started /etc/rc.local Compatibility.
参考:
https://www.jianshu.com/p/79d24b4af4e5
https://www.cnblogs.com/defifind/p/9285456.html