Check if background process is running or not

zisco

New Member
Joined
Mar 14, 2021
Messages
3
Reaction score
1
Credits
28
On my Synology DS215j I have a scheduled task, that is executed every 5 minutes and checks, if a specific python script is running:

Bash:
#!/bin/bash
# Import environment variables ...
. /etc/profile

PYSCRIPT="telegram-bot.py"
if ! ps ax | grep -q "$PYSCRIPT"; then
    echo "$PYSCRIPT is running"
else
   echo "$PYSCRIPT not running, attempting to start..."
   cd /volume1/homes/Python
   exec python3 "$PYSCRIPT"
   echo "$PYSCRIPT started!"
fi

Works principially fine, but the execution of the task never ends (except the python script crashes). So, I tried to start it in background exec python3 "$PYSCRIPT" &. But with this change, there is a huge disadvantage included, this line if ! ps ax | grep -q "$PYSCRIPT"; then now always returns FALSE, so that the python-script is started again and again...

Any ideas?
 


You could try using pgrep, so it looks something like this.
Bash:
#!/bin/bash
# Import environment variables ...
. /etc/profile

PYSCRIPT="telegram-bot.py"

pgrep "$PYSCRIPT" &> /dev/null
if [[ $? -eq 0 ]]
        then
            echo "$PYSCRIPT is running"
        else
            echo "$PYSCRIPT not running, attempting to start..."
            cd /volume1/homes/Python
            exec python3 "$PYSCRIPT"
            echo "$PYSCRIPT started!"
fi
With pgrep you check if the process is running, if it is running you will get a return value of zero and if it's not running you will get a return value greater than zero. This way if your return value is equal to zero your then will be executed and if your return value is greater than zero your else will be executed.
 
Last edited:
You could probably throw in an extra check in there so it looks like this.
Bash:
#!/bin/bash
# Import environment variables ...
. /etc/profile

PYSCRIPT="telegram-bot.py"

pgrep "$PYSCRIPT" &> /dev/null
if [[ $? -eq 0 ]]
        then
            echo "$PYSCRIPT is running"
        else
            echo "$PYSCRIPT not running, attempting to start..."
            cd /volume1/homes/Python
            exec python3 "$PYSCRIPT"
            pgrep "$PYSCRIPT" &> /dev/null
            if [[ $? -eq 0 ]]
            then
                echo "$PYSCRIPT started succesfully!"
            else
                echo "$PYSCRIPT failed to start"
            fi
fi
 
Last edited:
Thank you for the pgrep tip - but unfortunately the result is the same as with ps ax... - the script is started again and again! For some reason,the process - if run in the background - cannot be "seen" by the Task?
 
Then the problem is with the telegram-bot.py script because the test doesn't find a running process for telegram-bot.py and then tries to start it again, so it seems like as soon as telegram-bot.py is executed it also exits instead of that it keeps running in the background. No idea what that script does but maybe it's designed to only be run once to provide output and not as a background process? Try running telegram-bot.py manually and see what happens?
Code:
python3 telegram-bot.py
It should then give you some output or an idea what happens when that python script is launched by the bash script.
 
Last edited:
do some testing manually:
- start your background process from the command line
- manually do the 'ps' and see if it shows up - if it does, then that's not the problem, if it doesn't then the background script is probably failing immediately upon execution

if the process shows up as running then manually execute the script that you're scheduling and see what happens.

Just keep peeling the onion manually until you find a root cause.

keith
 
Finally, I've got the problem solved in the python-script itself. Using psutil and - as an important part - not only looking for the process, but also for the called cmd; the problem was, that the process-name is just python3 and in addition the name of the script is telegram-bot.py.
Thanks to all!
 

Members online


Top