testsrv script (SysV script)
I. Description:
CentOS7 Systemd have been used to manage the service, it is recommended to use Systemd management services. ubuntu18.04 is now Systemd management services.
init.d CentOS6 time is common, but now, CentOS6, CentOS7, ubuntu16.04, ubuntu18.04 still can be used.
The script is generally placed in the directory /etc/rc.d/init.d
The script can receive start, stop, status, reload, restart, etc. parameters management services
Basically will be loaded /etc/rc.d/init.d/functions, this document some of the more useful functions
#Describe two rows on #chkconfig and write, describe this now seemingly can not write.
#chkconfig:2345 96 07
- must state, in 2345 on behalf of these modes, open testsrv function, number 96 represents the number opened in 9607 when representatives closed, the equivalent of creating a soft link, and do not have the number of repeats, / etc /rc.d/rc5.d/ look at the existing number
That again, CentOS7 are not using init0-6 to manage user mode, also managed by systemd
Second, the title
Script writing services /root/bin/testsrv.sh, complete the following requirements
(1) Script acceptable parameters: start, stop, restart, status
(2) If the argument is not this one of the four, when prompted to use format error exit
(3) the case of start: create / var / lock / subsys / SCRIPT_NAME, and display "start success" Consider: If the advance has been started once, how to deal with?
(4) the case stop: delete the / var / lock / subsys / SCRIPT_NAME, and displays the "complete halt" to consider: If the advance is already stopped, and how to deal with?
(5) the case of restart, the first stop, and then start to consider: If you had not start, how to deal with?
(6) Status case, if the / var / lock / subsys / SCRIPT_NAME file exists, then the display "SCRIPT_NAME is running ...", if the / var / lock / subsys / SCRIPT_NAME file does not exist, then the display "SCRIPT_NAME is stopped. .. "
(7) It is prohibited in all modes to start the service, available chkconfig and service command management Description: SCRIPT_NAME name for the current script
Third, script
[root@imooc-nginx init.d]# cat /etc/init.d/testsrv
#!/bin/bash
# chkconfig: 2345 10 90
# description: testsrv
#
. /etc/init.d/functions
check_running(){
[ -e /var/lock/subsys/`basename $0` ] &&
STAT=0 ||
STAT=1
}
do_status(){
check_running
if [ "$STAT" = "1" ];then
action "`basename $0` is stopped..." false
else
action "`basename $0` is running..." true
fi
}
do_start(){
check_running
if [ "$STAT" = "0" ];then
action "`basename $0` 正在运行,取消操作..." true
exit 0
elif [ "$STAT" = "1" ];then
touch /var/lock/subsys/`basename $0`
fi
check_running
if [ "$STAT" = "0" ];then
action "`basename $0` 启动成功..." true
elif [ "$STAT" = "1" ];then
action "`basename $0` 启动失败..." false
exit 20
fi
}
do_stop(){
check_running
if [ "$STAT" = "0" ];then
rm -f /var/lock/subsys/`basename $0`
check_running
if [ "$STAT" = "1" ];then
action "`basename $0` 停止成功..." true
elif [ "$STAT" = "0" ];then
action "`basename $0` 停止失败..." false
exit 10
fi
fi
}
do_restart(){
check_running
if [ "$STAT" = "0" ];then
action "`basename $0` 已经运行,正在重新启动..." true
do_stop
do_start
elif [ "$STAT" = "1" ];then
action "`basename $0` 没有运行,正在启动..." true
do_start
fi
}
case "$1" in
start|stop|status|restart)
do_$1
;;
*)
echo "缺少参数: start|stop|status|restart"
;;
esac
4. Operation
CentOS7 operation
[root@imooc-nginx init.d]# mv testsrv.sh testsrv
#将testsrv加入SysV服务
[root@imooc-nginx init.d]# chkconfig --add testsrv
[root@imooc-nginx init.d]# chkconfig --list testsrv
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
testsrv 0:关 1:关 2:开 3:开 4:开 5:开 6:关
#上面可以看到testsrv加入到了2345模式下开机启动,和配置文件中写的一样
#将该服务在345模式下关闭
[root@imooc-nginx init.d]# chkconfig --level 345 testsrv off
[root@imooc-nginx init.d]# chkconfig --list testsrv
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
testsrv 0:关 1:关 2:开 3:关 4:关 5:关 6:关
Start close play
V. Detailed script
Tell you, CentOS7 recommended to systemd management services, management does not recommend using SysV
chkconfig 2345 10 90 testsrv service enabling functions expressed in four modes 2345, number 10 indicates turning off number represented 96,07.
This number should pay attention to the next, and the other can not repeat, init 1 is a single-user mode, this mode most of the services are switched off (K starts), / etc / rc.d / rc1.d / themselves below look at it, choose to open a useless number . init 5 is a desktop graphical mode, a lot of service is started, you can take a look at the following /etc/rc.d/rc5.d/ which opened number (S starts) occupied, select a number of closed
If you do not want to start the service in any mode, then turned into the 2345 -
At the same time explain once again: CentOS7 not use init0-6 to manage the startup mode, use Systemd management
Six, chkconfig usage
Now this is CentOS6 use, CentOS7 though still in command, but do not have to manage the service
View all services start-up situation
[root@qqq init.d]# chkconfig --list
Atd service open view of the situation to start
#2 3 4 5模式下开机启动
[root@qqq init.d]# chkconfig --list atd
atd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Atd service is prohibited under the 2345 models start
[root@qqq init.d]# chkconfig --list atd
atd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
New wrote testsrv, adding SysV
chkconfig --add testsrv
Should be deleted del