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
user5889117
  • 107
  • 1
  • 8
  • Have a look at [this question from earlier today](https://stackoverflow.com/questions/51600563/counting-process-instances-with-grep-wc) and the duplicates linked there. – Benjamin W. Jul 31 '18 at 04:24
  • Add output of `ps x |grep -v grep |grep -c "python3.6"` to your question. – Cyrus Jul 31 '18 at 04: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). – Amadan Jul 31 '18 at 04: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`) – David C. Rankin Jul 31 '18 at 04:49
  • output is 0 as no python.exe is running. – user5889117 Jul 31 '18 at 04:49
  • 1
    **output** and `'$?'` **return** are *two different things*... – David C. Rankin Jul 31 '18 at 04:51
  • Cyrus, output of ps x |grep -v grep |grep -c "python3.6" is 0 – user5889117 Jul 31 '18 at 04:53
  • @DavidC.Rankin: Oh, thanks! I was doubting my sanity for a bit, I was sure I tested it and it exited with status 0! I won't delete my previous brainfart to preserve the context of the discussion, but please ignore my comment above. – Amadan Jul 31 '18 at 09:12
  • Output is 0. Exit code (`$?`) is _not_. You never look at the output. – Amadan Jul 31 '18 at 09:14
  • @Amadan - welcome to the human race.... You know, you can delete that comment above `:)` But preservation for posterity is fine as well. – David C. Rankin Jul 31 '18 at 20:33

2 Answers2

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 ]
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 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? –  Jul 31 '18 at 04:30
  • @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. – Amadan Jul 31 '18 at 04:32
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
user5889117
  • 107
  • 1
  • 8