shell 脚本检查程序是否应该运行并在适当的情况下重新启动它

发布于 2024-12-13 08:13:23 字数 1892 浏览 0 评论 0原文

我有以下脚本

#!/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. 在外?

谢谢


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

#!/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: line 15: syntax error:unexpected end of file

因此 + $'\r' 返回之前的唯一内容是 #!/ bin/shset -vx

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

I have the following script

#!/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

This script should check if a daemon script is suppose to be running. If it is suppose to be running then it checks to see if the script is running and restarts it if it isn't. Currently I get syntax error: unexpected end of file when I try to running the file manually.

So two questions:

  1. why am i pulling the syntax error
  2. outside of this should this script run properly?

Thanks


EDIT:
here is an updated version of the script and a few notes:

#!/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

to note:

  • vim marks $(...) as a syntax error ( i don't know if that matters)
  • ps ax|grep -v grep|grep -q tracker_stream , ps ax|grep -v grep|grep tracker_stream , and cat stream_should_be_running.txt all execute properly from the command line

EDIT 2:
shell debugging gives the error

$ sh stream_checker.sh

+ 

stream_checker.sh: line 15: syntax error: unexpected end of file

so the only things that come before where the + $'\r' is returns are #!/bin/sh and set -vx.

This is running on a linux system. The my local machine is osx lion and the live machine is a linux server on webfaction.

\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: line 15: syntax error: unexpected end of file

so the only things that come before where the + $'\r' is returns are #!/bin/sh and set -vx.

This is running on a linux system. The my local machine is osx lion and the live machine is a linux server on webfaction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角卖回忆 2024-12-20 08:13:23

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) I think I got it...

I used the '-s' switch in pidof to only get one result.
The '-z' switch means "return true if the string is empty".

#!/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

EDIT: From the last note you posted it looks like you might have a Ctrl-M char (^M), somewhere on your file.
It's not merely a ^ followed by a M, it's a end of line character.

You could open your file with vim -b to check if you see any of those characters.
Then type:

:%s/^V^M//g 

That command reads like "match all (^M) chars and substitute them with void".
In short it will remove all (^M) chars from your file.
The (^V^M) bit means that you have to hit CTRL-V CTRL-M, in order to insert the (^M) char.

2) what exactly do you mean by "outside of this"?

梦途 2024-12-20 08:13:23

您的方括号周围需要空格,

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

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

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 调试。然后,您可以执行每行/代码块,以及替换变量的值。

我希望这有帮助。

your square brackets need spaces around them, i.e.

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

AND more importantly, you need to use command substitution so your script can get the value inside stream_should_be_running.txt using $( cat ... ), i.e.

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

Also better to dbl-quote the value returned by $(cat ...) in case some how there are spaces in the file.

Finally, turn on shell debugging by adding set -vx near top script. Then you can each line/block of code as it being executed, AND the values that are substituted for variables.

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文