650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1645231338-0.gif "style =" padding: 0px; margin: 0px; vertical-align: top; border: none; "alt =" "/> process_monitor.zip
#! /Bin/sh
# http://code.google.com/p/mooon# Process Monitoring script. When the specified process does not exist, run the restart script to pull it up. # features: #1. this monitoring script can be executed repeatedly and is automatically mutex-free to ensure that only one monitoring script process exists #2. mutex not only depends on the monitoring script file name, but also contains its command line parameters. It takes effect only when the whole file is mutually exclusive #3. for monitored processes, you can specify only the process name or include the command line parameter #4. whether it is a monitoring script or a monitored process, it is always only for processes belonging to the current user # command line parameters that need to be specified # parameter 1: name of the monitored process # parameter 2: restart the script of the monitored process if test $ #-ne 2; thenprintf "\ 033 [1; 33 musage: $0 process_cmdline restart_script \ 033 [m \ n "exit 1fi process_cmdline =" $1 "# Name of the process to be monitored, or the complete command line, you can also run some command line restart_script = "$2" # scripts to restart the process. You must have the executable permission monitor_interval = 10 # scheduled detection interval, the Unit is second cur_user = 'whoam' # the user name that executes this monitoring script # obtain the IP address on the specified Nic # eth = 1 & netstat-ie | awk-F' [:] ''in in {found = 0 ;}{ if (match ($0, "eth '" $ eth "'") found = 1; else if (1 = found) & match ($0, "eth") found = 0; if (1 = found) & match ($0, "inet addr:") & match ($0, "Bcast:") print $13 ;}' # The following script, used to prevent multiple monitoring script processes from appearing uid = 'id-u $ cur_user 'self _ name = 'basename $ 0' self _ cmdline = "$0 $ *" process_name = $ (basename 'echo "$ process_cmdline" | cut-d ""-f1 ') # periodically checks whether the specified process exists while true in an endless loop. doself_count = 'ps-C $ self_name h-o euser, args | awk' BEGIN {num = 0 ;} {if ($1 = uid | $1 = cur_user) & match ($0, self_cmdline) {++ num ;}} END {printf ("% d", num);} 'uid = $ uid cur_user = $ cur_user self_0000line = "$ self_0000line" 'If test $ self_count-gt 2; then # If the monitoring script is already running, exit and do not repeatedly run printf "\ 033 [0; 32; 31 m $0 is running [$ self_count], current user is $ cur_user. \ 033 [m \ n "exitfi # Check whether the monitored process exists. If not, restart process_count = 'ps-C $ process_name h-o euser, args | awk 'in in {num = 0 ;}{ if ($1 = uid | $1 = cur_user) & match ($0, process_cmdline )) {++ num ;}end {printf ("% d", num );} 'uid = $ uid cur_user = $ cur_user process_cmdline = "$ process_cmdline" 'if test $ process_count-lt 1; then # Run the restart script, this script is required to be able to pull the specified process printf "\ 033 [0; 32; 34 mrestart \ "$ process_cmdline \" \ 033 [m \ n "sh-c" $ restart_script "# Be sure to run fi sleep $ monitor_intervaldone in" sh-c "mode.
This article is from the "Fei Yue" blog, please be sure to keep this source http://mooon.blog.51cto.com/1246491/958837