shell 脚本检查程序是否应该运行并在适当的情况下重新启动它
我有以下脚本
#!/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
。
所以有两个问题:
- 如果这个脚本正常运行,为什么我要把语法错误排除
- 在外?
谢谢
编辑: 这是脚本的更新版本和一些注释:
#!/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_stream
、ps ax|grep -v grep|grep tracker_stream
和cat 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/sh
和 set -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:
- why am i pulling the syntax error
- 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
, andcat 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 我想我明白了...
我在 pidof 中使用了“-s”开关只得到了一个结果。
'-z' 开关表示“如果字符串为空则返回 true”。
编辑:从您发布的最后一条注释来看,您的文件中的某处可能有一个 Ctrl-M 字符
(^M)
。它不仅仅是一个
^
后跟一个M
,它是一个行尾字符。您可以使用
vim -b
打开文件来检查是否看到任何这些字符。然后输入:
该命令读起来像“匹配所有 (^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".
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 aM
, 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:
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"?
您的方括号周围需要空格,
即更重要的是,您需要使用命令替换,以便您的脚本可以使用
$( cat ... )stream_should_be_running.txt
内的值code>,即最好用 dbl 引用
$(cat ...)
返回的值,以防文件中存在空格。最后,通过在顶部脚本附近添加
set -vx
来打开 shell 调试。然后,您可以执行每行/代码块,以及替换变量的值。我希望这有帮助。
your square brackets need spaces around them, i.e.
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.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.