V
V
Viktor2020-02-05 17:27:48
bash
Viktor, 2020-02-05 17:27:48

Multiple launch of the script for no reason, where did you go wrong?

Good day experts!

I have a telegram bot written in python. In the bot's location folder, I created a crontab.sh file with the following content:

#!/bin/bash
if [ "pgrep -f bot.py" != 0 ]
  then {
    sleep 1  #delay
    /usr/bin/python3.6 /home/usertest/bot.py
    exit 1
  }
else
  {
    echo "Exit! Python bot is already running!"
    exit 1
  }
fi;

I made the crontab.sh file executable:
chmod u+x /home/usertest/crontab.sh

Next, I executed it under the current user
crontab -e
and added the following line:
*/5 * * * * /home/usertest/crontab.sh
for some reason, the bot starts in 2-3-4-5 and so on instances. Help to understand
Operating system ubuntu 18.04

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2020-02-06
@azbukait

#!/bin/bash
PIDFILE=/home/usertest/bot.pid

if pgrep --pidfile $PIDFILE &>/dev/null; then
    echo "Exit! Python bot is already running!"
    exit 1
else
  nohup /usr/bin/python3.6 /home/usertest/bot.py &>/dev/null &
  echo $!>$PIDFILE
fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question