Linux Daemon Automatically restarts shell script sharing

Source: Internet
Author: User
Tags log log sleep
#!/bin/sh #本脚本为守护进程, ,  automatic restart process   Function check_and_kill_and_start () {log_file=$1 If a process zombie is found
Name=$2 dead_duration=$3 # echo  "check the progress " $ps _name ymd=$ (date +%Y-%m-%d) Log_file=${log_file}${ymd}.log # echo  $log _file  to determine whether there is log_file if [ ! -e ${ Log_file} ];then exit 0 fi # %y  Last Modified time last_modify=$ (Stat -c %y ${log_file}) now =$ (date +%s) #如果上次写入日志的时间和现在的时间差大于  dead_duration  sec,  Restart process if [ $[  $now  -
  $last _modify ] -gt  $dead _duration ]; then mail_subject= ' [Alarm][${ps_name} process restart] ' Enter= ' \<br\> ' mail_content= ' ${ps_name} process restart ${enter} ' echo  ' ========================= ' $ (date +%) y-%m-%d %h:%m:%s ") ' ===================================== ' echo [$log _file] '  last update  time [' $ (date -d @ $last _modify  "+%y-%m-%d %h:%m:%s") '], need restart progress  ' [$ps _name] proc_id=$ (ps -ef|grep -i ${ps_name}|grep -v  "grep" |awk   ' {print $2} ') #echo   ' the old ps is  ' ${proc_id[@]} if [[ -n $ Proc_id ]];then echo  ' Old progress exist, kill it first ' for id in  ${proc_id[*]} do # thread=$ (ps -mp ${id}|wc -l)   #杀掉父进程和子进程 kill -term ${
ID}   if [ $? -eq 0 ];then echo  "Kill the progress sucessfull" else echo  "kill the progress failed " fi done echo  ' sleep l0s then  Start new progress ' fi   sleep 10s/usr/local/bin/python27 /var/disk/web/gsm_project/ score/${ps_name} & If [ $? -eq 0 ];then proc_id=$ (ps -ef|grep -i  ${ps_name}|grep -v  "grep" |awk  ' {print $2} ') echo  ' THE&NBSP;NEW&NBSP;PROGRESS&Nbsp;id is  ' ${proc_id[@]} else echo  ' Progress start failed, the cron will  try to start it next time  "fi #else #echo   ' ========================= ' $ ( date + "%y-%m-%d %h:%m:%s") ' ===================================== ' #echo  [$log _file] '  last  update time [' $ (date -d @ $last _modify  "+%y-%m-%d %h:%m:%s") '] ' fi}   # echo  ' ========================= ' $ (date + "%y-%m-%d %h:%m:%s") ' ================================== = = ' #执行方法  log file path   filename   Lookup time Check_and_kill_and_start /var/disk/logs/score_cache/server_gsm_ Score score_cache_match_5se.py 30




Linux Simple Shell creates its own daemon, automatically restarts, and the monitoring process runs


for daemon management, automatic restart, log log, There is a very useful process management tool Supervisord. It can be said to be simple and easy to use and powerful. But it's still too cumbersome for my deployment needs, and it doesn't fix how the process state is logged. The

wrote a simple shell script today to meet all my requirements and was simple to deploy. The

would like to provide a reference for code farmers who have the same needs.

pgmctl.sh #!/bin/bash cmd= "./mypgm" pid= "./log/pid.txt" log= "/log/mypgm.log" false "debug=----------------- ----------------------------------#  start function function start {     $CMD  - debug= $DEBUG  server.ini >>  $LOG  2>&1 &     
mypgmpid=$!     echo  $mypgmpid  >  $PID     echo  "start [ OK] "} #  stop function function stop {    kill  ' cat  $PID '      rm  $PID     echo  "Stop [ok]"} # ----------------------------------
----------------echo  "$CMD  $1" case  " in start"     start;;
Start_debug)     debug= "true"     start;; Restart)     if [ -f  $PID  ] ; then          Stop         sleep 4     fi    
 start;;
stop)     stop     exit 0;; Esac for  ( c=0 ; ; c++ ) do     if [ -f  $PID  ] ; then         mypgmpid= ' cat  $PID '          cmdex= "Ps uh -p$mypgmpid"          psrtn= ' $cmdex '         if [ -z  ' $psrtn '
 ]; then             #  process hangs out automatically reboot             echo  "' date  ' +%y/%m/%d  %h:%m:%s '  fatalerror restart service '  >>  $LOG             &nbsP;start         elif  ((  $c%20 == 0 ));  Then             #  record process running status              echo  "' date  ' +%y/%m/%d %h:%m:%s '   psinfo  $psrtn " >>  $LOG                c=0         fi        
 sleep 3      else         break     fi Done



Start:./pgmctl.sh start

Debug boot:./pgmctl.sh start_debug
Restart:./pgmctl.sh restart

Stop:./pgmctl.sh stop

This script is just an example, providing an idea. When you are using it, you need to modify the

to your own needs.

Related Article

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.