#!/bin/sh
# Http://code.google.com/p/mooon
# process Monitoring script, when the specified process does not exist, perform a restart script to pull it up
Characteristics
# 1. This monitoring script can be repeatedly executed, it will automatically do mutually exclusive, ensure that there is always only one monitoring script process exists
# 2. Mutex is not only based on the monitor script filename, but also contains its command-line arguments, only the whole is mutually exclusive.
# 3. For a monitored process, you can specify only the process name or the command line arguments
# 4. Whether it is a monitoring script or a monitored process, it is always targeted only at processes that belong to the current user
# command line arguments that require a specified number
# parameter 1: The process name being monitored
# parameter 2: Restart the script for the monitored process
if test $#-ne 2; Then
printf "\033[1;33musage: $ process_cmdline restart_script\033[m\n"
Exit 1
Fi
Process_cmdline= "$" The name of the process that needs to be monitored, or the complete command line, or a partial command line
Restart_script= "$" # a script to restart a process that requires executable permissions
MONITOR_INTERVAL=10 # timed to detect time interval, in seconds
Cur_user= ' whoami ' # User name for execution of this monitoring script
# Take the IP address on the specified network card
#eth =1&&netstat-ie|awk-f ' [:] ' begin{found=0;} {if (Match ($, "eth ' $eth")) found=1 else if (1==found) && match ($, "eth")) Found=0; if ((1==found) && Match ($, "inet addr:") && match ($, "bcast:")) print $} '
# The following script is used to prevent multiple monitoring script processes from appearing
Uid= ' id-u $cur _user '
Self_name= ' basename $ '
Self_cmdline= "$ $*"
process_name=$ (basename ' echo "$process _cmdline" |cut-d ""-f1 ")
# timed to detect whether a specified process exists in a dead loop
While true; Todo
Self_count= ' ps-c $self _name h-o euser,args| awk ' BEGIN {num=0;} {if ($1==uid | | $1==cur_user) && match ($, self_cmdline)) {++num}} End {printf ("%d", num);} ' uid= $uid cur_user= $cur _user self_cmdline= ' $self _cmdline '
if test $self _COUNT-GT 2; Then
# If the monitor script is already running, exit does not run repeatedly
printf "\033[0;32;31m$0 is running[$self _count], the current user is $cur _user.\033[m\n"
Exit
Fi
# Check if the monitored process exists and reboot if it does not exist
Process_count= ' ps-c $process _name h-o euser,args| awk ' BEGIN {num=0;} {if ($1==uid | | $1==cur_user) && match ($, process_cmdline)) {++num}} End {printf ("%d", num);} ' uid= $uid cur_user= $cur _user process_cmdline= ' $process _cmdline '
if test $process _count-lt 1; Then
# Perform a restart script that requires the script to pull up the specified process
printf "\033[0;32;34mrestart" $process _cmdline "\033[m\n"
Sh-c "$restart _script" # Note that the "sh-c" method must be performed
Fi
Sleep $monitor _interval
Done
This article is from the "Flying Month" blog, please be sure to keep this source http://mooon.blog.51cto.com/1246491/958837