1

I have a Python file that needs to be constantly running (a Telegram bot), but if I run in the background

python bot.py &

and then gracefully end the SSH connection, after a while the bot stops responding and I have to connect and launch it again.

How can I stop this from happening? If i cannot, how can I check when it is dead and relaunch it?

  • You sure the the instance you are using is not preemptible? – Rahul Chawla Oct 16 '18 at 13:47
  • @RahulChawla yes – ElectroCamel Oct 16 '18 at 13:59
  • Where exactly you were running this executable files from? If you were running it from the [Google Cloud shell](https://cloud.google.com/shell/docs/), this is one of its [usage limitation](https://cloud.google.com/shell/docs/limitations#usage_limits). Cloud Shell is intended for interactive use only. Non-interactive sessions will be ended automatically after a warning. – Digil Oct 16 '18 at 20:19
  • But, if you were running it inside from an instance and still want the command to be executed even after closing the shell, you have to use 'SCREEN', 'nohup' or 'disown'. You could check these discussion threads[1](https://unix.stackexchange.com/questions/134924/i-am-using-why-isnt-the-process-running-in-the-background)-[2](https://stackoverflow.com/questions/431521/run-a-command-in-a-shell-and-keep-running-the-command-when-you-close-the-session)-[3](https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and) about the usage of these commands. I hope this helps you. – Digil Oct 16 '18 at 20:19

1 Answers1

3

chmod +x bot.py

Do this to your python file to make it executable

nohup /path/to/script/bot.py &

The nohup will run the script in the background while the & will keep it running after you close the terminal

To check if your script is still running ps -e | grep bot.py

To see any errors run cat nohup.out

maxadorable
  • 1,290
  • 1
  • 10
  • 24