Ubuntu开机启动程序或脚本

本文介绍如何在Ubuntu中配置开机启动脚本,通过编辑rc.local文件和创建自定义的startup.sh脚本来实现开机自动运行Python脚本。文章详细解释了脚本的编写和执行流程,确保Python应用在系统启动时能够自动运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.写入开机脚本

rc.local脚本是一个ubuntu开机后会自动执行的脚本,利用这一点,实现Ubuntu的开机启动项。
目录:/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.

sh /home/startup.sh

exit 0

其中sh /home/startup.sh为自定义启动脚本

2.编写启动sh文件

此处以启动python脚本为例
创建startup.sh文件,并存放在/home/下

#!/bin/sh 
pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'` 
if [ "$pid" != "" ] 
then 
	echo "main.py already run, stop it first" 
	kill -9 ${pid} 
fi 
echo "starting now..." 
nohup python -u main.py > main.out 2>&1 & 
pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'` 
echo ${pid} > pid.out 
echo "main.py started at pid: "${pid}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值