0

我写了一个下面给出的shell脚本

unicorn_cnt=$(ps -ef | grep -v grep | grep -c unicorn)
if (( $unicorn_cnt == 0 )); then
 echo "Unicorn Stopped" | mail -s "Alert - Unicorn" someone@somedomin.com
fi
delayed_job_cnt=$(ps -ef | grep -v grep | grep -c delayed_job)
if (( $delayed_job_cnt == 0 )); then
 echo "Delayed Job Stopped" | mail -s "Alert - Unicorn" someone@somedomin.com
fi
rake_cnt=$(ps -ef | grep -v grep | grep -c rake)
if (( $rake_cnt == 0 )); then
  echo "Convertion Stopped" | mail -s "Alert - Unicorn" someone@somedomin.com
fi

这是用于检查进程是否正在运行,如果没有发送警报邮件。我对shell脚本不太熟悉。运行时显示以下错误。

process.sh: 3: process.sh: 2: not found
process.sh: 7: process.sh: 0: not found
process.sh: 11: process.sh: 0: not found

从我部分理解的一些研究中,这是因为创建变量时的空间问题。没有把握。我尝试使用一些解决方案,例如sedread。但它仍然显示错误。谁能帮我。

感谢和问候

4

3 回答 3

1

使用括号:

if [ "$unicorn_cnt" == 0 ]; then

或者最好这样写:

if ! ps -ef | grep -q [u]nicorn; then
 echo "Unicorn Stopped" | mail -s "Alert - Unicorn" someone@somedomin.com
fi

这意味着“检查 ps -ef 是否有独角兽,如果找不到,请执行此操作”

于 2013-02-04T10:31:55.407 回答
0

对于数字比较,您应该使用eqnot ==。用于[[条件表达式。使用here 字符串而不是echo在您的邮件命令中。

尝试这个:

if [[ $unicorn_cnt -eq 0 ]]; then
    mail -s "Alert - Unicorn" someone@somedomin.com <<< "Unicorn Stopped"
fi
于 2013-02-04T10:49:33.067 回答
0

从上面的提示中,我找到了答案。

unicorn_cnt=$(ps -ef | grep -v grep | grep -c unicorn)
if [ $unicorn_cnt -eq 0 ]; 
then
  echo "Unicorn Stopped" | mail -s "Alert - Unicorn" someone@somedomin.com
fi
delayed_job_cnt=$(ps -ef | grep -v grep | grep -c delayed_job)
if [ $delayed_job_cnt -eq 0 ]; 
then
  echo "Delayed Job Stopped" | mail -s "Alert - Delayed Job" someone@somedomin.com
fi
rake_cnt=$(ps -ef | grep -v grep | grep -c rake)
if [ $rake_cnt -eq 0 ]; 
then
  echo "Convertion Stopped" | mail -s "Alert - Convertion" someone@somedomin.com
fi

它现在工作正常,我们也可以将它与 cronjob 集成。

于 2013-02-04T10:56:47.453 回答