Shell script TestSrv script (init script or sysv script)

testsrv script (SysV script)

I. Description:

? CentOS7 already uses Systemd to manage services, and it is recommended to use Systemd to manage services. ubuntu18.04 is now also a Systemd management service.

? init.d is commonly used in CentOS6, but now, CentOS6, CentOS7, ubuntu16.04, ubuntu18.04 can still be used.

? The script is generally placed in the /etc/rc.d/init.d directory

? The script can receive start, stop, status, reload, restart and other parameters. Management service

? Basically will load /etc/rc.d/init.d/functions, there are some useful functions in this file

? #chkconfig and #describe Both of these lines must be written, and it seems that you don’t need to write describe now.

? #chkconfig:2345 96 07——It must be stated, 2345 means that the testsrv function is turned on in these modes, 96 means that the number is 96 and 07 means that the number is turned off , It is equivalent to creating a soft connection, this should not be repeated with the existing number, /etc/rc.d/rc5.d/ see the existing number

? Let me explain, CentOS7 is not used now init0-6 to manage the user mode, and also use systemd to manage

Second, topic

Write the service script /root/bin/testsrv.sh and complete the following requirements

(1) The script can accept parameters: start, stop, restart, status

(2) If the parameter is not one of the four, it will prompt to use the format and report an error to exit

(3 ) If it is start: create /var/lock/subsys/SCRIPT_NAME, and display “startup successful”. Consider: If it has been started once beforehand, what should I do?

(4) If it is stop: delete /var/lock/subsys/SCRIPT_NAME and display “Stop Complete”. Consider: If it has been stopped beforehand, what should I do?

(5) If it is restart, stop first, then start. Consider: If there is no start, how to deal with it?

(6) If it is status, then if the /var/lock/subsys/SCRIPT_NAME file exists, it will display “SCRIPT_NAME is running…”, if the /var/lock/subsys/SCRIPT_NAME file does not exist, Then it displays “SCRIPT_NAME is stopped…”

(7) It is prohibited to start the service in all modes. You can use the chkconfig and service commands to manage. Description: SCRIPT_NAME is the current script name

Three , Script

[[email protected] 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` is running, cancel operation..." true
exit 0
elif ["$STAT" = "1" ];then
touch /var/lock/subsys/` ba sename $0`
fi
check_running
if ["$STAT" = "0" ]; then
action "`basename $0` started successfully..." true
elif ["$STAT" = "1" ];then
action "`basename $0` failed to start..." false
exit 20
fi
}

do_stop(){
check_running
if ["$STAT" = "0" ];then
rm -f /var/lock/subsys/`basename $0`< br /> check_running
if ["$STAT" = "1" ];then
action "`basename $0` stopped successfully..." true
elif ["$STAT" = " 0" ];then
action "`basename $0` Stop failed..." false
exit 10
fi
fi
}

do_restart(){
check_running
if ["$STAT" = "0" ];then
action "`basename $0` is already running, restarting..." true
do_stop
do_start
elif ["$STAT" = "1" ];then
action "`basename $0` is not running, starting..." true
do_start
fi
}

case "$1" in
start|stop|status|restart)
do_$1
;;
*)
echo "Missing parameter: start|stop|status|restart"
;;
esac

Four. Operation

CentOS7 operation

[[emailprotected] init.d] # mv testsrv.sh testsrv
#Add testsrv to SysV service
[[emailprotected] init.d]# chkconfig --add testsrv
[[emailprotected] init.d]# chkconfig --list testsrv

Note: This output only shows the SysV service and does not include the native systemd service. SysV configuration data
may be overwritten by the native systemd configuration.

To list systemd services, execute ‘systemctl list-unit-files’.
To view the services enabled in a specific target, please execute
‘systemctl list-dependencies [target]’.

testsrv 0: Off 1: Off 2: On 3: On 4: On 5: On 6: Off
# Above you can see that testsrv has been added to the 2345 mode to start up, and configuration The file says the same
#Turn off the service in 345 mode
[[emailprotected] init.d]# chkconfig --level 345 testsrv off
[[emailprotected] init. d]# chkconfig --list testsrv

Note: This output only shows the SysV service and does not include the native systemd service. SysV configuration data
may be overwritten by the native systemd configuration.

To list systemd services, execute ‘systemctl list-unit-files’.
To view the services enabled in a specific target, please execute
‘systemctl list-dependencies [target]’.

testsrv 0: Off 1: Off 2: On 3: Off 4: Off 5: Off 6: Off

Start and close play p>

1566898703978

Five. Detailed scripting

? Furthermore, CentOS7 recommends systemd to manage services, but SysV management is not recommended

? chkconfig 2345 10 90 means that the service opens testsrv in four modes of 2345, 10 means The open number is 96, and 07 represents the closed number.

? This number should be noted, it cannot be repeated with others, init 1 is a single-user mode, in this mode most of the services are turned on and off (starting with K), /etc/rc.d /rc1.d/ Let’s take a look at it for yourself, choose a useless opening number. Init 5 is a desktop graphical mode. Many services are activated. You can see which open numbers (starting with S) under /etc/rc.d/rc5.d/ are occupied. Select a closed number

? If you don’t want the service to start in any mode, then change 2345 to –

? At the same time, let me explain: CentOS7 does not use init0-6 to manage startup Mode, use Systemd management

Six, chkconfig usage

This is now used by CentOS6. Although the commands are still there, CentOS7 is no longer used to manage services

p>

Check the startup status of all services

[[emailprotected] init.d]# chkconfig --list

Check the startup status of the atd service

#2 3 4 5 boot in the mode
[[emailprotected] init.d]# chkconfig --list atd
atd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Forbid atd service to start in 2 3 4 5 mode

[[email Protected] init.d]# chkconfig --list atd
atd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

I wrote a new testsrv and added SysV

chkconfig --add testsrv

Delete should be del

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5527 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.