Objective: Linux server programs will be dumped for various reasons, which will affect user usage. Here we provide a simple process monitoring and restart function.
Implementation principle: the script is called by the scheduled task crontab. The script uses PS to check whether the process exists. If not, restart and write logs.
1. Modify crontab
chen@IED_40_125_sles10sp1:~/CandyAT/Bin> crontab -e*/1 * * * * /home/chen/CandyAT/Bin/monitor.sh
The above indicates that the script monitor. Sh is called every minute.
2. Implementation of monitor. Sh
#! /Bin/shhost_dir = 'echo ~ '# Current user root directory proc_name = "candygameserver" # process name file_name = "/candy/log/cron. log "# Log File pid = 0proc_num () # Number of computing processes {num = 'ps-Ef | grep $ proc_name | grep-V grep | WC-l' return $ num} proc_id () # process number {pid = 'ps-Ef | grep $ proc_name | grep-V grep | awk '{print $2} ''} proc_numnumber = $? If [$ number-EQ 0] # determine whether the process has then CD $ host_dir/candyat/bin /;. /candy. sh-dzone # restart the process command. Modify proc_id # obtain the New Process Code echo $ {pid }, 'date'> $ host_dir $ file_name # record the new process number and restart time fi.