How would I go about telling my web server (Webfaction) to just run a python file and let it keep running forever? Someone told me I should use cron
but I haven't found any good material on how to run it once, and have it not stop.
How would I go about telling my web server (Webfaction) to just run a python file and let it keep running forever? Someone told me I should use cron
but I haven't found any good material on how to run it once, and have it not stop.
From what I understand, you want to keep your process running after close ssh. You could use
screen
.The absolute easiest way is to use something like nohup to make the process a daemon:
http://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html
what you need to do is:
nohup ./path_to_your_script.sh &
once you do that you can logout, close the terminal, whatever and your script will continue running until the server is restarted or you kill the process
Cron runs periodically forever. Cron jobs can be run by the minute, hour, daily, weekly, or monthly. Here is an extensive reference:
http://unixgeeks.org/security/newbie/unix/cron-1.html
Here's a quick reference:
http://adminschoice.com/crontab-quick-reference
Running in screen is fine for testing, but at some point it will break for one reason or another (WebFaction does like to kill processes when you use too much memory) if then you'd need to go restart it automatically.
Supervisord (http://supervisord.org/ ) is pretty good for running things and restarting them if they fail for whatever reason. It also handles "daemonizing" processes which is what it's called when you detach a process from the current terminal so that it will carry on running after you've quite your season (when you log out of SSH), it is possible to write code in your Python script to do this yourself but why bother when there are awesome tools like Supervisord?
Of course there's always the possibility that Supervisord will die (or be killed). This is where you could use Cron. Cron let's you run something periodically. A nice property of the way that Supervisord is usually set up is that if you try and start it while it's already running it won't actually start a second time so you if you set up a Cron task to start Supervisord every 5 minutes then you should be ok.
The best practice method for something like this is:
1) Write the script such that it first checks for a lock file to ensure there are no other copies already running and/or check that the process is not already running.
2) Put an entry in cron to start the script every so often, say once an hour.
3) Your script could use a while(1) loop to keep itself running "forever". However, it is best to never have a script run forever since you could have potential memory leaks that accumulate over a long time and eventually cause your system to swap and then you have a really slow system. Have it run for many thousands of iterations or kill itself after a certain amount of time. Cron will kick it off again on the next timeslot.
4) Log your output using >> redirect to a log file so you can catch any messaging you may want to output for status.