Process management tool under Linux—supervise (monitors and restarts dead programs)

Keyword: Process management tool under Linux - supervise (monitoring and restarting dead programs)


supervise: Articles that can be referred to by the system process monitoring manager 

( refer to this article to install successfully ):
1. Install the supervise service in linux: http: //blog.csdn.net/xvshu/article/details/51792668
2. Note: The run script does not have any suffix
1. The tool supervise

Daemontools is a software package that contains many tools for managing Unix services. The most core tool is supervisor, its function is to monitor a specified service and restart the process when the service process dies. It is very easy to add a service monitored by supervisor, just add a directory of the service to be monitored, and add a script file named run that starts the server in the directory.
The svscan tool starts a supervisor process for each subdirectory in all subdirectories under the specified working directory (default is /service/ directory), and can start up to 1000 supervisor processes at most (that is, in the working directory). can have up to 1000 subdirectories). In each of these subdirectories, there will be a script program named run to start the corresponding service. Supervise monitors the service and uses the run script to automatically start the service when it dies. If the sticky bit of a subdirectory under the working directory of svscan is set, svscan will start two supervisory processes for the subdirectory, one to monitor the service corresponding to run in the subdirectory, and the other to monitor the log subdirectory under the subdirectory. The record service between the two is connected to each other through a pipeline.
Svscan detects subdirectories every 5 seconds. If a new directory appears, start supervisor for that directory. If the supervisor corresponding to an old subdirectory exits, restart it.
Please refer to the online documentation for details of all tools in this package. The most classic combination of daemontools is to use it with lighttpd

2. Install the

  /pacakage directory (you can create any directory, the package is used here just to keep it consistent with the English author):  
  mkdir -p /package
  chmod 1755 /package
  cd /package

  Download daemontools -0.76.tar.gz to /package directory, decompress the package.

http://cr.yp.to/daemontools/daemontools-0.76.tar.gz  


  tar xvzf daemontools-0.76.tar.gz
  cd admin/daemontools-0.76

  Compile and install the daemontools program
  
  package/

install The prompt of installation failure appears because daemontools needs a patch, daemontools-0.76.errno.patch, which is included in the qmail package. Or modify the daemontools source code to fix this bug

(modification method: add the following code at the end of the first line of the conf-cc file under src - include /usr/include/errno.h

# vi src/conf-cc

add -include /usr/include/errno.h at the end

)

If the installation is successful, you can use the following command to confirm:

# ps -ef | grep svscan
# man svscan

At this point, you can check the inittab file :
# cat /etc/inittab

will find that daemontools uses init to protect itself:

SV:123456:respawn:/command/svscanboot

You can see that the system checks the service every five seconds through the strace command:
# strace -p `pidof svscan`


4. Use the supervisor program for program management and monitoring

  . The execution command of supervisor is supervisor Path, where Path is the specified path, which can be a relative path or an absolute path. Under the Path path, there must be a run script, which is called by supervisor and monitors and manages the programs running in the script.

  An important function of supervise is to detect whether the program executed in the run script is working normally. If it is found that it has died, supervise will re-execute the run script and restart the specified program. This is very necessary for many server programs, and no one wants to crawl out of bed at 2 o'clock in the middle of the night to restart the server.

  Below is a simple example of using supervisor.

V. 1) Example 1

  Assuming that daemontools has been installed, create a test directory, enter the directory
  
  mkdir /temp1
  cd /temp1

  and write a simple test program test.c in this directory:  

  compile test.c and output it as test.  
  gcc -o test test1.c

  Write a script run to execute the test program so that supervisor can call it. 

  

#!/bin/sh  

echo "start test!"  

./test Return

  to the parent directory, execute supervisor temp1 to see the effect:
  
  cd ..
  supervise temp1

  execute killall -9 test, kill the test process, you will find that supervisor will restart test process. Of course, if the program core dumps, supervisor will also restart the program.     C codeCopy codeFavorite   

code

    #include #include     int main(){     int ix = 0;      for(;; ix++){       printf("%d\n", ix);     sleep(1);      }      return 0; 








    }

Note: When you stop and start supervisor again to monitor a certain directory, it will prompt:

supervise: fatal: unable to acquire /service/test/supervise/lock: temporary failure

, then delete the supervisor in the directory and monitor again.

2) Example 2 (java)

command: mkdir /service/test

cd /service/test

ll

vi demo.java

Java code copy code favorite code

    class demo{
    public static void main(String[] args) throws Exception{
    for(int i =0;;i++){
    System.out.println("i="+i);
    Thread.sleep(1000);
    }
    }
    }

javac -d . demo.java

vi run

Xml codeCopy codeFavorite code

    #!/bin /sh
    echo -e "start test";
    exec java demo

chmod +x run

executes the monitoring directory: supervise /service/test

[the terminal prints out the label]

and then opens a terminal to view the process id that is executing the command, execute ps -A

to find the id number of the java process,

execute killall -9 java

Look at the previous terminal, whether printing has started again, huh, huh. This means that supervise starts the process again after the interruption.

3) Example 3

mkdir /tmp/test

cd /tmp/test

vi demo.java

[The code is the same as example 2]

javac -d . demo.java

vi run

Java codeCopy codeFavorite code

    # !/bin/sh
    echo -e "start test2";
    exec java -classpath /tmp/test demo

chmod +x run

ln -s /tmp/test /service/test

ll /service

supervise /service/test

found that it started to print, At this time, execute killall -9 java in another terminal, and you will find that the printing of this terminal starts again, that is, immediately after the process is killed, run is executed again.

4) Example 4 Monitoring tomcat startup

If tomcat

creates a new run file in the /service directory of /var/tomcat6 in redhat, the content is as follows:
Java code copy code favorite code

    #!/bin/sh
    TOMCAT_HOME=/var/tomcat6
    exec ${ TOMCAT_HOME}/bin/catalina.sh run

executes supervisor /service

and finds that tomcat is started.

Test: Open a new terminal, ps -ef |grep tomcat

to find the id of tomcat, execute kill -9 [tid]

to kill tomcat, but the previous terminal window shows that tomcat is restarted.

[Note]: It seems that the ./shutdown.sh command of tomcat cannot restart the monitoring. If an exception occurs, the port is occupied. It may be that the startup command is executed before tomcat closes the monitoring.

References: 1) Official website: http://cr.yp.to/daemontools.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483728&siteId=291194637