Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix: Have Python script constantly running best practice?

I have a Python script which process data off of a HTTP data stream and I need this script to in theory be running at all times and forever unless I kill it off manually to update it and run it again.

I was wondering what the best practice to do this was on a Unix (Ubuntu in particular) so that even if Terminal is closed etc the script continues to run in the background unless the process or server are shut down?

Thanks

like image 551
Mo. Avatar asked Jun 09 '15 20:06

Mo.


2 Answers

From your question you are implying that you are going to start the script from your terminal and not by any of Linux's startup script management methods like systemd or upstart or init.d scripts.

If you intend to start your script from terminal, and you want it to continue to run after you close your terminal, you need to do two things

1. Make it run in the background by appending '&' after your script.

2. When you close the terminal, the shell associated to it sends HUP signal to all the processes before it dying. To ignore the HUP signal and continue to run in the background you need to start your script with 'nohup'.

tl;dr
Run your script this way:

$ nohup python mypythonscript.py &
like image 140
Ram Avatar answered Sep 25 '22 03:09

Ram


Adding your script to rc.local would work, but 'best practice' in my opinion would be to use Upstart. See this post:

Daemon vs Upstart for python script

like image 37
Jorick Spitzen Avatar answered Sep 24 '22 03:09

Jorick Spitzen