jes516
jes516

Reputation: 552

Bash if statement only proves true once

First off, my apologies if the question is relatively broad. Perhaps someone can re-word it accordingly.

I wrote the script below to elaborate on my bash skill however the if statement only proves true if I manually start the python script it is meant to "monitor".

bash script = run_deamon.sh
python script = deamon.py

If I manually run ./deamon.py and then manually run ./run_deamon.sh the bash script echos out "Python deamon.py running...". I can then proceed to kill the deamon.py process and my bash script echos " Starting Python deamon.py...". After the bash script starts deamon.py it no longer echos "Python deamon.py running..." Can someone explain what I am doing wrong?

I would like the bash script to output:

Python deamon.py running...
Python deamon.py running...
Python deamon.py running... (I now kill deamon.py)
Starting Python deamon.py...
Python deamon.py running...
Python deamon.py running...

etc...

I tried the following:

while true; do                                                                  
    if [[ $(ps ax | grep 'deamon.py$') ]]; then                                 
        echo Python deamon.py running...;                                       
    else                                                                        
        echo Starting Python deamon.py...;                                      
        /home/bang/Documents/Python/./deamon.py;                                
    fi                                                                          
    sleep 1                                                                     
done 

and

while true; do                                                                  
    if [[ $(ps ax | grep 'deamon.py$') ]]; then                                 
        echo Python deamon.py is running...;                                    
    fi                                                                          

    if ! [[ $(ps ax | grep 'deamon.py$') ]]; then                               
        echo Starting Python deamon.py...;                                      
        /home/bang/Documents/Python/./deamon.py;                                
    fi                                                                          

    sleep 1;                                                                                              
done  

same behavior.

If someone can point me in the right direction it is greatly appreciated.

Edit #1

I used

if [[ $(ps ax | grep 'deamon.py$' | grep -v grep) ]]; then

and

if [[ $(ps ax | pgrep -f 'deamon.py$' ]]; then

Same result. I added a print statement to my python script and now the bash script is echoing the print statement:

23
24
25
Starting Python deamon.py...
0
1
2
3

This is my python script:

from time import sleep                                                          

x = 0                                                                           

while True:                                                                     
    print(x)                                                                    
    if x == 25:                                                                 
        exit(0)                                                                 
    x += 1                                                                      
    sleep(1) 

Upvotes: 0

Views: 63

Answers (2)

Munir
Munir

Reputation: 3612

You need to run the deamon.py process in the background and return control to the script like:

/home/bang/Documents/Python/./deamon.py & ;

Otherwise, the script will wait for the deamon.py to finish before continuing.

Upvotes: 3

Paul Hicks
Paul Hicks

Reputation: 14019

Exclude the grep from the grep.

When you run ps ax | grep 'deamon.py$' then deamon.py$ will be in the ps list. You are getting a false positive: the daemon might not be running, but you'll never know because you'll always match against the grep.

A brute force method would be to change your shell command to

ps ax | grep 'deamon.py$' | grep -v grep

Upvotes: 0

Related Questions