2013-11-26 78 views
0

所以,我有一個在Linux上運行的應用程序。不幸的是,該應用程序並不像它需要的那樣穩定。所以,我需要保姆,並確保它正常工作。我的應用程序立即分出6個進程。我想出了一個可行的方案,但我想知道是否有更好的方法來實現這一點。所以,我的腳本每分鐘都會執行一次cron作業。我的腳本需要:Cron-Bash:如何在Linux上照顧一個應用程序

  1. 確保初始過程仍在運行。
  2. 確保列表中的6支叉正在運行。

這裏是劇本我到目前爲止:

#!/bin/sh 

#myprocess -s will return the status of the running process 
output=`myprocess -s`; 

if [ "$output" != "No myprocess found." ] ; then 
    myprocess_pid=`echo $output | cut -d":" -f2`; 

    #checks if that pid is running 
    myprocess_running=`ps | grep $myprocess_pid | grep myprocess`; 

    #if the string is an empty string, means script isn't running anymore. 
    if [ -z "$myprocess_running" ] ; then 

    #fires it off 
    echo "`date` - myprocess not running ...." >> /tmp/log/messages; 
    myprocess; 

    fi 
else #it ain't running 

    echo "`date` - output from myprocess -s: $output" >> /tmp/log/messages;  
    myprocess; 

fi 

這怎麼可能提高?

感謝您的任何建議!

+0

如果你有一個簡短的PID,那麼'ps | grep $ pid'可能會返回一些進程的PID(或名稱,就此而言)是您正在查找的PID的子字符串。最好使用'ps -p「$ pid」'。 – tripleee

回答

1
  1. myprocess -s可以設置返回碼!= 0;你可以寫

    if myprocess -s; then 
        ... code-when-ok ... 
    else 
        ... code-when-failure ... 
    fi 
    
  2. 當你有PID,你可以看看在/proc/<pid>,而不是孩子的你ps .. | grep ... | grep

  3. ,當你有父PID,你可以grep "^Ppid:[[:space:]]*$pid" /proc/*/status和計數輸出線,以確定一些進程

相關問題