ubuntu设置开机启动

1,ubuntu18.04,开机使用root启动程序

感谢原作者

1),需要建立一个rc-local的service文件

sudo vim /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
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target

2),ExecStart指向的内容我们需要建好

sudo vim /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
/home/xxx/autoStart.sh &
##以上代码是个人自己测试的时候自己写的一个sh脚本 exit
0

3),新建好自己的脚本和输入内容

#!/bin/sh
python3 xxx.py &
exit 0

并且给予权限

4),启动服务并且检查状态

sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

5),重启,检查是否成功

cat /usr/local/test.log
ps -aux|grep python3    #如果python运行时间比较久的话

 

2,ubuntu16,开机启动

这个比较简单,直接把要跑的脚本或者命令加到/etc/rc.local里就行了。有关后台运行,或者nohup什么的,自行注意。

#!/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.
python /home/center/webroot/temp/udp_server.py &
#echo "hehe" > /usr/local/test.log &
exit 0

 

posted @ 2023-04-24 16:12  0点0度  阅读(277)  评论(0编辑  收藏  举报