How to automatically restart the monitoring program in Linux

Source: Internet
Author: User
In Linux, the monitoring program is automatically restarted. to restart a mobile phone, follow these steps: ---- Linux restart command ---- Java code 1, shutdown2, poweroff3, init4, reboot5, and halt are not described here, mainly about automatic restart... in Linux, the monitoring program is automatically restarted. to restart a mobile phone, follow these steps: ---- Linux restart command ---- Java code 1, shutdown 2, poweroff 3, init 4, reboot 5, and halt are not described here, mainly about the implementation of automatic restart. The simplest shell script that comes to mind when you use a script to enable automatic restart is as follows: ps-ef | grep "$1" | grep-v "grep" | wc-l is the number of processes that get $1 ($1 indicates the name of the process, the script determines the next operation based on the number of processes. Through an endless loop, check the number of processes of the specified program in the system every one second. you can also use crontab here. This method can solve the problem basically, but with a latency of 1 s, I did not use this method in the application. for this shell script, please refer to the attachment code after the article. The exec + fork method is implemented using the exec + fork method. the specific idea is as follows: 1. the exec function replaces the current process with a new process, the new process is specified by the path or file parameter. You can use the exec function to switch the execution of a program from one program to another. 2. the fork function creates a new process and creates a new table item in the table, the creator (that is, the parent process) continues to execute according to the original process, and the child process executes its own control process; 3. when the wait fork starts a child process, the child process has its own lifecycle and runs independently. we can call the wait function in the parent process to wait for the child process to end, the reader can think of a solution: 1) first use the fork system to call and create a sub-process; 2) use the exec function in the sub-process to execute the program that needs to be restarted automatically; 3) execute wait in the parent process to wait for the child process to end, and then create a new child process. Usage: Java code #. /portmap the path of the program to be monitored # parameters required by args portmap $. /supervisor. /portmap args ..... the code is as follows:/*** supervisor *** author: liyangguang (liyangguang@software.ict.ac.cn) *** date: 21:04:01 ** changes * 1, execl to execv */# include # Include # Include # Include # Include # Include # Include Int main (int argc, char ** argv) {int ret, I, status; char * child_argv [100] = {0}; pid_t pid; if (argc <2) {fprintf (stderr, "Usage: % s N ", argv [0]); return-1 ;}for (I = 1; I <argc; ++ I) {child_argv [i-1] = (char *) malloc (strlen (argv [I]) + 1); strncpy (child_argv [i-1], argv [I], strlen (argv [I]); child_argv [i-1] [strlen (argv [I])] = '';} while (1) {pid = fork (); if (pid =-1) {fprintf (stderr, "fork () error. errno: % d error: % sn ", errno, strerror (errno); break;} if (pid = 0) {ret = execv (child_argv [0], (char **) child_argv); // ret = Execl (child_argv [0], "portmap", NULL, 0); if (ret <0) {fprintf (stderr, "execv ret: % d errno: % d error: % sn ", ret, errno, strerror (errno); continue;} exit (0);} if (pid> 0) {pid = wait (& status ); fprintf (stdout, "wait return") ;}} return 0 ;}the code in shell script mode is as follows: the code is copied as FOLLOWS # function: CheckProcess # function: check whether a process exists # Parameter: $1 --- name of the process to be checked # return: If yes, 0 is returned; otherwise, 1 is returned. #------------------------------------------------ ---------------------------- CheckProcess () {# check whether the input parameter is valid if ["$1" = "]; then return 1 fi # $ PROCESS_NUM get the number of specified process names, if the value is 1, 0 is returned, indicating normal. if the value is not 1, 1 is returned, indicating that an error exists, you need to restart the Java code PROCESS_NUM = 'PS-ef | grep "$1" | grep-v "grep" | wc-L' if [$ PROCESS_NUM-eq 1]; then return 0 else return 1 fi} # check whether the test instance already exists while [1]; do CheckProcess "test" CheckQQ_RET =$? If [$ CheckQQ_RET-eq 1]; then # kills all the test processes. you can change the killall-9 test exec./test & fi sleep 1 done
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.