Simple implementation of Linux Process Monitoring and Automatic Restart

Source: Internet
Author: User

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.

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.