0

Below is my script and this script is design to work to check if "python3.6" process is running.

If it is not running then execute python code else exit 0.

I don't see any activity or process running of python from this bash script.

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
10
  • Have a look at this question from earlier today and the duplicates linked there. Commented Jul 31, 2018 at 4:24
  • Add output of ps x |grep -v grep |grep -c "python3.6" to your question. Commented Jul 31, 2018 at 4:36
  • My answer was utterly wrong because I forgot about the -c. grep -c never exits with an error, whether found or not (as long as there wasn't something else like missing file that caused one), and $? is always 0 regardless of how the search went. grep needs to succeed at searching; grep -c needs to succeed at counting. I have no idea why your code wouldn't be running, because I'd expect it to run always (ignoring whether or not grep found anything). Commented Jul 31, 2018 at 4:40
  • 1
    You were right for the right reason... grep -c will have a return of 1 if the input is empty. (e.g. echo "" | grep -c "python3" returns 1) Commented Jul 31, 2018 at 4:49
  • 1
    output and '$?' return are two different things... Commented Jul 31, 2018 at 4:51

2 Answers 2

1

if [ $? -eq 0 ]; then means "If the last command exited without error". grep returns an error result when it can't find stuff; so your if says "If you found a Python process, run another Python process, otherwise never mind". So your code doesn't find the Python process, and so never launches one (mistakenly believing solitary life is no life at all).

You can change it in multiple ways. You can change -eq to -ne.

Or you don't even need to compare the exit code explicitly, because that's what if does. 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 ]
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, just wanted to check something, on my machine if I do grep python3.6 does not do anything, do you on Ubuntu have to specify exact version or what?
@Gox: Of course you don't have to. But if OP's version is indeed Python 3.6, it will work for them. I just repeated it because this is likely not the problem OP is asking about, given that a much more serious and harder to spot problem exists in the if statement.
0

Thank you all for your help & kind cooperation.

With comments suggestions, i did minor change & my bash is running fine.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.