ubuntu 18.04开机自启动

2019-10-27  本文已影响0人  四阿哥萌萌哒

在linux开机自启动中通常可以添加脚本在rc.local中,所以需要自己生产并注册

1.Ubuntu18.04生成rc-local.service

sudo gedit /etc/systemd/system/rc-local.service
将以下内容复制进rc-local.service文件

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local  #脚本文件位置
[Service]
Type=forking
ExecStart=/etc/rc.local start  #配置的脚本文件rc.local为start
TimeoutSec=0    
StandardOutput=tty  #标准输出
RemainAfterExit=yes
SysVStartPriority=99  #优先级,当有多个开机启动文件时可以设置不同的值
[Install]
WantedBy=multi-user.target

2.生成rc.local

sudo gedit /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.
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
exit 0

3.给rc.local加上权限,启用服务,启动服务并检查状态

sudo chmod +x /etc/rc.local
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

重启并检查test.log文件
cat /usr/local/test.log

4.利用rc.local文件配置开机自启动脚本

sudo gedit /etc/rc.local
打开脚本文件,然后在exit 0前面加入需要执行的代码即可。
然后再执行下面代码(18.04需要,其他系统可能不需要,可以不执行,重启试一下)

sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
上一篇下一篇

猜你喜欢

热点阅读