[英]ubuntu bash script not running
以下是我的脚本,该脚本旨在检查“ python3.6”进程是否正在运行。
如果未运行,则执行python代码,否则退出0。
我从该bash脚本中看不到任何python的活动或进程运行。
请帮助我了解我是否编写了错误的脚本。
任何帮助是极大的赞赏。
在此先感谢。
#!/bin/bash
ps x |grep -v grep |grep -c "python3.6"
if [ $? -eq 0 ]; then
bash -c "/home/ubuntu/anaconda3/bin/python3.6 /var/lib/pythoncode/main_progm.py >> /home/ubuntu/Logs.txt"
fi
# End of Script
if [ $? -eq 0 ]; then
if [ $? -eq 0 ]; then
表示“如果最后一个命令没有错误退出”。 grep
找不到东西时会返回错误结果; 因此您的if
表示“如果找到了Python进程,请运行另一个Python进程,否则不要紧”。 因此,您的代码找不到Python进程,因此永远也不会启动它(错误地认为孤零零的生活根本就没有生命)。
您可以通过多种方式进行更改。 您可以将-eq
更改为-ne
。
或者甚至不需要显式比较退出代码,因为if
可以。 您可以这样写:
if ps x | grep -v grep | grep -c python3.6
then
# grep found stuff and exited with $? == 0
echo Oops, still running
else
# grep failed and exited with $? != 0
echo Good to go, run it
fi
或者,您可以使用产生的计数:
pythoncount=$(ps x |grep -v grep |grep -c "python3.6")
if [ $pythoncount -eq 0 ]
谢谢大家的帮助和友好的合作。
有评论建议,我做了些小改动,bash运行良好。
下面是代码,我希望它可以帮助某些人进入类似情况。
再次感谢你。
#!/bin/bash
ps -x |grep -v grep |grep -c "python3.6" >/dev/null
if [ $? -ne 0 ]; then
bash -c "/home/ubuntu/anaconda3/bin/python3.6 /var/lib/pythoncode/main_progm.py >> /home/ubuntu/Logs.txt"
else
exit 0;
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.