Linux 本身并没有提供添加开机启动的方法,但有时总是需要一些脚本或者代码开机启动,本文介绍两种方法,将自己的程序或者几行简单的代码添加为开机启动项,使其在后台自动执行。
关于 Linux 的启动,其实有一套严格的流程,请阅读Linux 的启动流程。
以下均以 CentOS7
系统为例,Ubuntu
会特殊说明。
1. 构建一个开机启动服务
该方法适用于较大型的开机启动项,可以书写完整的
start|stop|restart|status
等命令。
创建脚本
开机启动项脚本被 Linux 存放于 /etc/init.d/
路径下,我们在此目录下创建一个 test
示例脚本,将需要执行的代码全部写入该脚本中。1
2cd /etc/init.d
touch test
在继续之前,先看一下开机启动脚本的一个模板:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
### BEGIN INIT INFO
# Provides: [程序名称,唯一]
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: [启动项的简短说明]
# Description: [启动项的完整说明]
### END INIT INFO
[需要执行的命令]
exit 0
其中,Provides
是程序的名称,必须唯一;Default-Start
与 Default-Stop
是开机启动项的运行级别(runlevel),详细请见参考1。
接着,开始书写脚本来实现一个功能:运行一个简单 Python 死循环脚本,并把结果写到 log
里,包括了 start|stop|restart|status
方法。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
### BEGIN INIT INFO
# Provides: test_init_program
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: [启动项的简短说明]
# Description: [启动项的完整说明]
### END INIT INFO
NAME="test_init_program"
NAME_BIN="test_core.py"
FOLDER="/root/"
BIN="/root/test_core.py"
Info_font_prefix="\033[32m"
Warning_font_prefix="\033[33m"
Error_font_prefix="\033[31m"
Font_suffix="\033[0m"
RETVAL=0
check_running(){
PID=`ps -ef |grep "${NAME_BIN}" |grep -v "grep" |grep -v "init.d" |grep -v "service" |awk '{print $2}'`
if [[ ! -z ${PID} ]]; then
return 0
else
return 1
fi
}
start(){
check_running
if [[ $? -eq 0 ]]; then
echo -e "${Warning_font_prefix}[WARNING]${Font_suffix} $NAME (PID $PID) has started running!"
else
cd $FOLDER && nohup python -u $BIN > test_core.log 2>&1 &
check_running
if [[ $? -eq 0 ]]; then
echo -e "${Info_font_prefix}[INFO]${Font_suffix} $NAME (PID $PID) started successfully!"
else
echo -e "${Error_font_prefix}[ERROR]${Font_suffix} $NAME started failed!"
fi
fi
}
stop(){
check_running
if [[ $? -eq 0 ]]; then
kill -9 ${PID}
RETVAL=$?
if [[ $RETVAL -eq 0 ]]; then
echo -e "${Info_font_prefix}[INFO]${Font_suffix} $NAME (PID $PID) stopped successfully!"
else
echo -e "${Error_font_prefix}[ERROR]${Font_suffix} $NAME stopped failed!"
fi
else
echo -e "${Warning_font_prefix}[WARNING]${Font_suffix} $NAME is not running!"
RETVAL=1
fi
}
status(){
check_running
if [[ $? -eq 0 ]]; then
echo -e "${Info_font_prefix}[INFO]${Font_suffix} $NAME (PID ${PID}) is running..."
else
echo -e "${Info_font_prefix}[INFO]${Font_suffix} $NAME is not running!"
RETVAL=1
fi
}
restart(){
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo -e "Usage: $0 {start|stop|status|restart}"
RETVAL=1
;;
esac
exit $RETVAL
其中,/root/test_core.py
文件内容为死循环更新文件内的一个数字:1
2
3
4
5
6
7
8import time
count = 0
while True:
open("./test_result.txt", "w").write("{:d}".format(count % 10000))
count += 1
time.sleep(1)
保存之后,为该脚本添加可执行权限:1
chmod +x test
注:需要后台静默运行的程序,需使用 nohup
命令放到后台执行。
设置开机启动
不同的系统使用不同的命令来管理开机启动项。CentOS
使用 chkconfig
命令来管理开机启动项,而 Ubuntu
使用 update-rc.d
或 sysv-rc-conf
。
CentOS
使用 chkconfig
1 | chkconfig test on # 设置为开机启动项 |
chkconfig
命令用法还有很多,如指定运行级别等,可以通过 man chkconfig
来查看。
Ubuntu
使用 update-rc.d
1 | update-rc.d test defaults # 设置为开机启动项 |
同样,update-rc.d
也支持指定运行级别。
手动管理服务
添加在 /etc/init.d/
目录下的可执行文件会被系统自动识别为 systemd
服务,可以使用 systemctl
命令进行管理:1
2
3
4systemctl start test
systemctl stop test
systemctl restart test
systemctl status test
当然也可使用脚本进行管理:1
2
3
4/etc/init.d/test start
/etc/init.d/test stop
/etc/init.d/test restart
/etc/init.d/test status
2. 添加开机启动项代码
该方法适用于较小型的开机启动项,简单几行代码运行完即结束。
Linux 在开机执行完 systemd
服务后还会执行 /etc/rc.local
文件中的命令,所以可以直接将需要开机启动的小型命令写到这里。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
# 将需要执行的命令写到这里
# 如:
echo "2019-10-10" >> /root/local.log
参考
Linux 的启动流程
https://blog.ilemonrain.com/linux/linux-startup-run.html