通过/etc/rc.local实现开机自动拉起服务

时间:2021-08-28 12:51:50

添加服务到/etc/rc.local

如自动拉起apache服务:

/etc/rc.local:

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff. touch /var/lock/subsys/local
/usr/sbin/apachectl start

服务是关闭的

[root@limt ~]# chkconfig --list|grep http
httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭 [root@limt httpd]# reboot Broadcast message from root@limt
(/dev/pts/1) at 21:25 ... The system is going down for reboot NOW!

开机后http进程已经启动:

[root@limt ~]# ps -ef|grep http
root 2907 1 0 21:29 ? 00:00:00 /usr/sbin/nss_pcache 163842 off /etc/httpd/alias
root 2909 1 0 21:29 ? 00:00:00 /usr/sbin/httpd -k start
root 2984 2909 0 21:29 ? 00:00:00 /usr/bin/crlhelper 196611 2909 /etc/httpd/alias
apache 2987 2909 0 21:29 ? 00:00:00 /usr/sbin/httpd -k start
apache 2988 2909 0 21:29 ? 00:00:00 /usr/sbin/httpd -k start
apache 2989 2909 0 21:30 ? 00:00:00 /usr/sbin/httpd -k start
apache 2990 2909 0 21:30 ? 00:00:00 /usr/sbin/httpd -k start
apache 2991 2909 0 21:30 ? 00:00:00 /usr/sbin/httpd -k start
apache 2992 2909 0 21:30 ? 00:00:00 /usr/sbin/httpd -k start
apache 2993 2909 0 21:30 ? 00:00:00 /usr/sbin/httpd -k start
apache 2994 2909 0 21:30 ? 00:00:00 /usr/sbin/httpd -k start
apache 2995 2909 0 21:30 ? 00:00:00 /usr/sbin/httpd -k start

对于有些服务最好在启动前先停止服务,因为有些服务进程有LOCK文件,如果异常停机后LOCK文件没有删除,在随机启动的时候会报服务已启动,通过在启动前先停止服务开消除LOCK文件来正常启动服务

如:

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff. touch /var/lock/subsys/local
/usr/sbin/apachectl stop
/usr/sbin/apachectl start