Ubuntu开机启动程序或脚本

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}

你可能感兴趣的:(Ubuntu)