Linux Monitoring program-Program automatic Restart implementation method

Source: Internet
Author: User
Tags strlen

If it's a simple phone reboot we can do the following

----Linux Restart command----

1, shutdown
2, Poweroff
3. Init
4, reboot
5, halt

Here we will not introduce, because I mainly talk about automatic restart implementation method


using scripts to automate reboots

First of all think of the simplest use of shell script, probably thinking:

Ps-ef | grep "$1″| Grep-v "grep" | Wc–l is the number of processes that get the name of the process, and the script determines the next action based on the number of processes. Through a dead loop, check the number of processes in the system for a specified program every 1 seconds, which can also be implemented using CRONTAB.

This method compares soil, still can basically solve a problem, but have 1s of delay, the author does not adopt this method in the application, regarding this shell script, see the attachment code after article.

Exec+fork Way
The author finally adopts the Exec+fork way to realize, the concrete thought is as follows:

The 1,EXEC function replaces the current process with a new process that is specified by the path or the file parameter. You can use the EXEC function to switch the execution of a program from one program to another;

The 2,fork function is to create a new process, create a new table entry in the process table, and the creator (that is, the parent process) to continue executing in the original process, and the child process executes its own control process;

3,wait when a child process is started by fork, the child process has its own lifecycle and will run independently, and we can call the wait function in the parent process to have the parent process waiting for the end of the child process;

Believe that the introduction here, the reader has been able to think of the solution: 1 first uses the fork system call, creates the subprocess, 2 uses the EXEC function in the subprocess, executes the program that requires automatic restart, 3 executes the wait waiting child process in the parent process, and then creates a new child process.

How to use:

The code is as follows Copy Code

#./portmap the path of the program that needs to be monitored
#args parameters required by Portmap
$/supervisor./portmap args ..... The code is as follows:

/**
*
* Supervisor
*
* Author:liyangguang (liyangguang@software.ict.ac.cn)
*
* Date:2011-01-21 21:04:01
*
* Changes
* 1, Execl to EXECV
*/

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

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 <exe_path> <args...>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 for the shell script is as follows:

The code is as follows Copy Code

# function: checkprocess
# Feature: Checks whether a process exists with the
# parameter:---The process name to check
# returns: 1 if there is a return of 0.
#---------------------- --------------------------------------------------------
Checkprocess ()
{
  # Check the parameters entered are valid
  IF ["$" = "];
  Then
    return 1
  fi
 
  # $PROCESS _num Gets the number of specified process names, 1 returns 0, indicating normal, does not return 1 for 1, indicates an error and needs to be restarted
  process_num= ' ps-ef | grep ' | grep-v ' grep ' | wc-l '
  if [$PROCESS _num-eq 1];
  then
    return 0
  Else
    return 1
  fi
}
& nbsp
 
# To check if the instance of test already exists
while [1], do
 checkprocess "test"
 checkqq_ret=$?
 if [$CheckQQ _ret-eq 1];
 then
 
# kills all test processes for any action you need to perform
 
 
  killall-9 test
  EXEC ./test & 
 fi
 sleep 1
Done

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.