简体   繁体   English

ubuntu bash脚本未运行

[英]ubuntu bash script not running

Below is my script and this script is design to work to check if "python3.6" process is running. 以下是我的脚本,该脚本旨在检查“ python3.6”进程是否正在运行。

If it is not running then execute python code else exit 0. 如果未运行,则执行python代码,否则退出0。

I don't see any activity or process running of python from this bash script. 我从该bash脚本中看不到任何python的活动或进程运行。

please help me to understand if i scripted something wrong. 请帮助我了解我是否编写了错误的脚本。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanking in advance. 在此先感谢。

#!/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 means "If the last command exited without error". if [ $? -eq 0 ]; then表示“如果最后一个命令没有错误退出”。 grep returns an error result when it can't find stuff; grep找不到东西时会返回错误结果; so your if says "If you found a Python process, run another Python process, otherwise never mind". 因此您的if表示“如果找到了Python进程,请运行另一个Python进程,否则不要紧”。 So your code doesn't find the Python process, and so never launches one (mistakenly believing solitary life is no life at all). 因此,您的代码找不到Python进程,因此永远也不会启动它(错误地认为孤零零的生活根本就没有生命)。

You can change it in multiple ways. 您可以通过多种方式进行更改。 You can change -eq to -ne . 您可以将-eq更改为-ne

Or you don't even need to compare the exit code explicitly, because that's what if does. 或者甚至不需要显式比较退出代码,因为if可以。 You could write: 您可以这样写:

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

Or you can use that count that you produced: 或者,您可以使用产生的计数:

pythoncount=$(ps x |grep -v grep |grep -c "python3.6")
if [ $pythoncount -eq 0 ]

Thank you all for your help & kind cooperation. 谢谢大家的帮助和友好的合作。

With comments suggestions, i did minor change & my bash is running fine. 有评论建议,我做了些小改动,bash运行良好。

Below is the code, i hope it will help somebody might step into similar situation. 下面是代码,我希望它可以帮助某些人进入类似情况。

thank you again. 再次感谢你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM