Linux Ubuntu 18.04 设置开机自动运行脚本 开机自启命令

Keva
阅读 364

第一步设置权限:chmod +x /etc/rc.d/rc.local

vim /etc/rc.d/rc.local

最下方插入要执行的命令即可,如:

sh /home/jmult_web/jmult-cropper/start.sh start

 

手动创建可执行的启动脚本

vi /etc/systemd/system/rc-local.service

插入以下内容

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Alias=rc-local.service

因为上面的路径指向的是/etc/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.
 
# 这里就会防止脚本,执行启动命令的位置
# 例如:
# echo "hello world" >> /root/123.log
# /bin/sh /root/123.sh
 
exit 0

然后给文件赋权

sudo chmod +x /etc/rc.local

然后加入系统程序中

sudo systemctl enable rc-local

这条语句就是创建一个超链接,在系统启动服务程序中

到这里添加开机启动已完成了,如果想不重启,直接查看效果执行如下命令即可

启动服务并检查状态

sudo systemctl start rc-local.service

sudo systemctl status rc-local.service

回到顶部