shell脚本,用于检查程序是否应该运行并在适当时重新启动它

时间:2011-11-03 20:47:31

标签: bash shell scripting

我有以下脚本

#!/bin/sh
if [cat stream_should_be_running.txt == 'true']; then #file will either contain true or false
    if [ps ax|grep -v grep|grep tracker_stream]; then # check if stream is currently running
        exit 0
    else
        /usr/local/bin/python2.7 ~/webapps/dashboard/fbadmin/manage.py tracker_stream; # restart stream
        exit 0
else
    exit 0
fi

此脚本应检查是否正在运行守护程序脚本。如果它正在运行,那么它会检查脚本是否正在运行,如果不是则重新启动它。目前,当我尝试手动运行文件时,我得到syntax error: unexpected end of file

所以有两个问题:

  1. 为什么我会拉出语法错误
  2. 这个脚本应该正常运行吗?
  3. 由于


    编辑: 这是脚本的更新版本和一些注释:

    #!/bin/sh
    set -vx; # turn on shell debugging
    
    if [[ "$(cat stream_should_be_running.txt)" == "true" ]]; then
        if [ ps ax|grep -v grep|grep -q tracker_stream ]; then
            exit 0
        else
            /usr/local/bin/python2.7 ~/webapps/dashboard/fbadmin/manage.py tracker_stream;
            exit 0
        fi
    else
        exit 0
    fi
    

    注意:

    • vim将$(...)标记为语法错误(我不知道是否重要)
    • ps ax|grep -v grep|grep -q tracker_streamps ax|grep -v grep|grep tracker_streamcat stream_should_be_running.txt都可以从命令行正确执行

    编辑2: shell调试给出了错误

    $ sh stream_checker.sh
    
    + $'\r'
    : command not foundline 3: 
    if [[ "$(cat stream_should_be_running.txt)" == "true" ]]; then 
        echo 'test';
        if [ ps ax|grep -v grep|grep -q tracker_stream ]; then 
            exit 0
        else
        /usr/local/bin/python2.7 ~/webapps/dashboard/fbadmin/manage.py tracker_stream;
        exit 0
        fi
    else
        exit 0
    fi
    

    stream_checker.sh:第15行:语法错误:意外的文件结尾

    因此+ $'\r'返回#!/bin/shset -vx之前的唯一事情是{{1}}。

    这是在linux系统上运行的。我的本地机器是osx lion,实时机器是webfaction上的linux服务器。

2 个答案:

答案 0 :(得分:1)

1)我想我明白了......

我在pidof中使用'-s'开关只得到一个结果。
'-z'开关表示“如果字符串为空则返回true”。

#!/bin/sh
PID=$(pidof -s tracker_stream);
if [ $(cat stream_should_be_running.txt) = "true"]; then #file will either contain true or false
    if [ -z $PID ]; then # check if stream is currently NOT running
        /usr/local/bin/python2.7 ~/webapps/dashboard/fbadmin/manage.py tracker_stream; # restart stream
        exit 0;
    fi
fi

编辑:从您发布的最后一个注释看起来,您的文件中可能有一个Ctrl-M字符(^M)
它不仅仅是^后跟M,它还是行尾字符。

您可以使用vim -b打开文件,检查是否看到任何这些字符。 然后输入:

:%s/^V^M//g 

该命令读起来像“匹配所有(^ M)字符并用void替换它们” 简而言之,它将从您的文件中删除所有(^ M)字符 (^ V ^ M)位意味着您必须按CTRL-V CTRL-M才能插入(^ M)字符。

2)你的意思是“在这个之外”?

答案 1 :(得分:1)

你的方括号需要它们周围的空格,即

if [ ps ax|grep -v grep|grep tracker_stream ] ;

更重要的是,您需要使用命令替换,以便您的脚本可以使用stream_should_be_running.txt$( cat ... )内获取值,即

if [[ "$(cat stream_should_be_running.txt)" == 'true' ]] ; then 
    #file will either contain true or false
    if[ ps ax|grep -v grep|grep -q tracker_stream ] ; then 
        # check if stream is currently running
        exit 0
    else
        /usr/local/bin/python2.7 ~/webapps/dashboard/fbadmin/manage.py tracker_stream; 
        # restart stream
        exit 0
else
    exit 0
fi

对于dbl引用$(cat ...)返回的值,如果文件中有多个空格,也更好。

最后,通过在顶部脚本附近添加set -vx来打开shell调试。然后,您可以在执行时执行每行/代码块,以及替换变量的值。

我希望这会有所帮助。