Recent work has used the Daemontools tool to manage the service process, which is summarized again.
When the service process hangs for some reason (including kill), Daemontools will restart the process
Environment Introduction:
CentOS 6.9 x86_64 bit minimized installation
First, package download installation process 1.1 Download package decompression
[[email protected] ~]#wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz [[email protected] ~]# tar xf daemontools-0.76.tar.gz[[email protected] ~]# cd admin/daemontools-0.76/
Edit SRC/CONF-CC file Add-include/usr/include/errno.h after file to prevent error during subsequent compilation and installation process
[[email protected] daemontools-0.76]# vim src/conf-cc gcc -O2 -Wimplicit -Wunused -Wcomment -Wchar-subscripts -Wuninitialized -Wshadow -Wcast-qual -Wcast-align -Wwrite-strings -include /usr/include/errno.h
Note: The package for the last command below represents the package directory under the daemontools-0.76 directory, not a package command
1.2sudo Compile and install
[[email protected] daemontools-0.76] #sudo package/install linking./src/* into./compile ... Compiling everything in./compile...sh find-systype.sh > Systyperm-f compilesh print-cc.sh > Compilechmod 555 compi Le./compile byte_chr.c ........... Env-/bin/sh rts.tests 2>&1 | Cat-v > Rtsrm-f sysdepscat systype compile load >> sysdepsgrep sysdep direntry.h >> sysdepsgrep sysdep ha Swaitp.h >> sysdepsgrep sysdep hassgact.h >> sysdepsgrep sysdep hassgprm.h >> sysdepsgrep SYSDEP Select. H >> sysdepsgrep sysdep uint64.h >> sysdepsgrep sysdep iopause.h >> sysdepsgrep sysdep hasmkffo.h >&G T Sysdepsgrep sysdep hasflock.h >> sysdepsgrep sysdep hasshsgr.h >> sysdepscopying commands into./command ... Creating symlink daemontools, daemontools-0.76 ... Making Command Links In/command ... Making Compatibility links In/usr/local/bin ... Creating/service ... Adding Svscanboot to Inittab...init should start Svscan now.Viewing Inittab, you can see this line at the end of the file: [[email protected] daemontools-0.76]# tail-1/etc/inittab sv:123456:respawn:/command/ Svscanboot
sudo is used because the install script creates two directories in the root directory: the/service and/command directories.
Where the/service directory is used to store processes that are managed by Daemontools.
Note that only connections are stored in the/service directory.
The/command command holds some common commands for daemontools.
Second, the configuration Daemontools
Configure Startup items to add CSH-CF '/command/svscanboot & ' to the end of/etc/rc.local=>/etc/rc.d/rc.local: Boot from
[[email protected] command]# sed -i ‘$acsh -cf \x27\/command\/svscanboot &\x27‘ /etc/rc.local[[email protected] command]# tail -2 /etc/rc.local csh -cf ‘/command/svscanboot &‘[[email protected] command]# sh /etc/rc.local/etc/rc.local: line 15: csh: command not found
Error, so install CSH command
[[email protected] command]# yum install tcsh -y[[email protected] command]# which tcsh/bin/tcsh[[email protected] command]# which csh/bin/csh
Install successfully again:
[[email protected] command]# sh -x /etc/rc.local + bash /var/lib/subsys/local+ csh -cf ‘/command/svscanboot &‘[1] 13689[[email protected] command]# [[email protected] command]# ps -ef|grep svsroot 13689 1 0 22:55 pts/3 00:00:00 /bin/sh /command/svscanbootroot 13694 13689 0 22:55 pts/3 00:00:00 svscan /serviceroot 13768 9451 0 22:55 pts/3 00:00:00 grep --color svs
or run it directly in the background.
[[email protected] service]# /command/svscanboot &[[email protected] service]# ps -ef|grep svsroot 1044 7763 0 22:37 pts/2 00:00:00 grep --color svsroot 30451 9451 0 22:34 pts/3 00:00:00 /bin/sh /command/svscanbootroot 30458 30451 0 22:34 pts/3 00:00:00 svscan /service
from the output of Ps-ef | grep SVS, you can see that Svscan is running as a svscanboot child process. and the Svscan Listener directory is the/service directory.
Iii. Brief introduction of Daemontools
Daemontools is a daemon tool that prevents user processes from exiting unexpectedly. Daemontools's tools are as follows:
3.1, the common Command introduction:
(1) Svscanboot
This command does not normally need to be entered manually, as this will normally start automatically with the startup of the Linux system. So join the start-up:/etc/rc.local will be up until it starts with Linux.
Svscanboot starts after startup Svscan,svscan will start supervise,supervise will start and listen to the run executable file in the/service directory where the service is stored.
(2) Svscan
Svscan will start the supervise process for all services under the/service directory. Svscan will start a pair of supervise services for each service: a S subdirectory (the project directory of the service process), which is actually a soft connection, and a log directory under the S subdirectory (if any). Supervise executes the run script under the S subdirectory and listens to the process started by the run script, and starts the run script in the S/log directory. The run script in this S/log directory is used to output the log of the S directory run script to the specified directory. Svscan Every 5s check the/service directory, if there is a new service, start a new supervise, if there is a supervise unexpectedly quit will restart supervise.
(3) supervise
The run script used by supervise to perform services in the/service directory. If the run script exits, it will execute every few seconds. Under/service, under Service directory S, there will be a supervise directory that holds the binary format information for the status of the process. Svstate will read this status message.
(4) The SVC command
Svc opts Services
opts is a series of parameters, and services refers to the service directory under/service. The opts parameter is as follows:
-u:up, if the service is not running, start it and restart it if the services stop.
-d:down, if the service is running, sends a term (terminate) signal to it, and then sends a cont (continue) signal, after it stops, no longer starts it.
-o:once, if the service does not run, starts it, but does not start after it has stopped. is to run only once.
-p:pause, which sends a stop signal to the services.
-c:continue, which sends a cont signal to the services.
-h:hang up to send a hup signal to the services.
-a:alarm, which sends a ALRM signal to the services.
-i:interrupt, which sends an INT signal to the services.
-t:terminate, which sends a term signal to the services.
-k:kill, sending a kill signal to the services.
-x:exit, supervise exits immediately after services are stopped, but it's worth noting that if you use this option in a stable system, you're already making a mistake: supervise is designed to run forever.
shutting down a service process typically uses the-DK parameter, at which point the supervise process is not closed, so the service process can still be restarted. Even if the supervice process hangs, Svscan will still restart the run script Supervise,supervise will rerun the service process.
Interface Svc OPTs Services opts is a series of getopt-style options. Services consists of any number of arguments, each argument naming a directory used by supervise. SVC applies all the options to each service in turn. Here is the options:-u:up. If The service is not running, start it. If The service stops, restart it. -d:down. If The service is running, send it a term signal and then a CONT signal. After it stops, does not restart it. -o:once. If The service is not running, start it. Do not restart it if it stops. -p:pause. Send the service a STOP signal. -c:continue. Send the service a CONT signal. -h:hangup. Send the service a HUP signal. -a:alarm. Send the service an ALRM signal. -i:interrupt. Send the service an INT signal. -t:terminate. Send the service a term signal. -k:kill. Send the service a KILL signal. -x:exit. Supervise would exit as soon as the service is down. If You use this option on a stable system, you ' re doing something wrong; Supervise is designed to RUn Forever.
(5) Svstat
[[email protected] service]# ps -ef|grep svsroot 13689 1 0 22:55 pts/3 00:00:00 /bin/sh /command/svscanbootroot 13694 13689 0 22:55 pts/3 00:00:00 svscan /serviceroot 21765 7763 0 23:06 pts/2 00:00:00 grep --color svs查看服务的状态:[[email protected] nginx]# svstat /service/nginx/service/nginx: up (pid 19169) 0 seconds
Iv. Examples of demonstrations:
Configuring a service is simple:
1) Create a directory with a run script
2) The Run script executes the command to start the service
3) Establish/service Next link
[[email protected] service]# mkdir /data/nginx -p[[email protected] service]# cat /data/nginx/run #!/bin/bashexec service nginx start
Stop Nginx Service
[[Email protected] nginx]# service Nginx stop
[[email protected] service]# chmod +x /data/nginx/run [[email protected] service]# ln -sv /data/nginx /service/nginx`/service/nginx‘ -> `/data/nginx‘
The magic is the last sentence, once the link is created, Daemontools will automatically help you start the Nginx service. Just stopped the Nginx service, and then the service was started again:
Such as:
To stop monitoring a service:
[[email protected] service]# svc -d /service/nginx[[email protected] service]# svstat /service/nginx/service/nginx: down 213 seconds, normally up停止nginx服务后,nginx服务不再被重启[[email protected] nginx]# service nginx stop
If you no longer need daemontools to guard a service, you can complete the above operation and delete the corresponding directory under/service.
Service Directory/service
To enable monitoring of nginx services:
[[email protected] service]# svc -u /service/nginx[[email protected] service]# svstat /service/nginx/service/nginx: up (pid 23563) 1 seconds
Nginx Service is started again
[[email protected] nginx]# ps-ef|grep nginxroot 13695 13689 0 22:55 pts/3 00:00:00 readproctitle Service Err ORS:.. dy running.? Starting nginx ... nginx (PID 23367 23366 23365 23364 23363) already running.? Starting nginx ... nginx (PID 23367 23366 23365 23364 23363) already running.? Starting nginx ... nginx (PID 23367 23366 23365 23364 23363) already running.? Starting nginx ... nginx (PID 23367 23366 23365 23364 23363) already running.? Starting nginx ... nginx (PID 23367 23366 23365 23364 23363) already running.? Root 13741 13694 0 22:55 pts/3 00:00:00 supervise nginxroot 23363 1 0 23:19? 00:00:00 nginx:master process/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.confwww 23364 23363 0 23:1 9? 00:00:00 nginx:worker process www 23365 23363 0 23:19? 00:00:00 nginx:worker process www 23366 23363 0 23:19? 00:00:00 Nginx:Worker process www 23367 23363 0 23:19? 00:00:00 nginx:worker Process root 23820 9451 0 23:20 pts/3 00:00:00 gr EP--color Nginx
Here's the end of the presentation, it's easy, and the partners look at it.
CentOS6.9 Installing the Daemontools tool Management service process