Bash script to execute python program if its not running

197 views Asked by At

I have the following bash script to check if my python script miner_nbeats.py is currently running and if not, then re-run because sometimes my python script gets killed due to out of memory error. So I have this bash script below, which works and executed the program at the beginning. However, I noticed that it’s not really re-running the python script after it gets killed.

PATH=/opt/conda/bin:/opt/conda/condabin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
while true; do

    if /bin/pgrep -f "miner_nbeats.py" | grep -v $$ >/dev/null; then
        echo "script running"
    else
        echo "script not running"
        tmux new-session \; send-keys "source activate python310 && cd /home/putsncalls23/directory && python miner_nbeats.py” Enter
    fi
    
    sleep 300
done

So I checked the htop program afterwards when miner_nbeats.py has been killed but the bash script isn't re-executing the python script since it is failing the first part of the if statement. Moreover, when I check pgref -f miner_nbeats.py, it would return me the pid thats associated with the bash script (or the tmux terminal ?)

Hence, I believe that the system is detecting the associated pid from running the bash script.

As an example, when I first run the bash script, it would help me start a new tmux window with the command entered, however, eventually my python script would get killed in the tmux window, and the bash script fails to re-execute the python script.

When I enter the pgrep -f miner_nbeats.py it would show a pid of 14826, which is the pid associated with the bash script in htop.

Also the reason I am using the tmux command is because I would like to have the python script running in the background even if my ssh session disconnects from the terminal to the linux server.

I am relatively new to bash scripting and would appreciate any help. Thank you

-------EDIT-----------

Thank you for the recommendation on systemd, this is something very new to me and after reading up on systemd, I figured to use something like this:

[Unit]
Description=Mining service for nbeats
After=network.target

[Service]
Type=simple
User=putsncalls23
WorkingDirectory=/home/putsncalls23/directory
ExecStart=/opt/conda/envs/python310/bin/python miner_nbeats.py 
Restart=always
RestartSec=300

[Install]
WantedBy=default.target

Would really appreciate if you could help verify, and it is very helpful for me to learn this new tool since I was stuck in the bash scripting world.

1

There are 1 answers

13
KamilCuk On

Start a systemd service with autorestarting.

It is better to create it system wide in /etc/systemd/system/miner_nbeats.service with the following content. The important thing is to set property Restart=always.

[Unit]
Description=starts my miner_nbeats
[Service]
ExecStart=/bin/bash -c 'source activate python310 && cd /home/putsncalls23/test && python miner_nbeats.py'
Restart=always
[Install]
WantedBy=multi-user.target

And then execute as root:

systemctl daemon-reload
systemctl enable miner_nbeats
systemctl start miner_nbeats

Alternatively, you could start the service ad-hoc with systemd-run.