Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Jul 30 '11 at 0:10

This question came from our site for professional and enthusiast programmers.

    
Your Python file could take care of the "running forever" part with a while loop or something along those lines. –  user43170 Jul 28 '11 at 3:54
    
I realize this. When I ssh into my server, run the file, then close the ssh window, however, the file stops. Or when I try to run it in the background with &, it also stops. –  tekknolagi Jul 28 '11 at 3:56
    
It's a very bad idea to keep running a process forever. In any case if you do want keep running it, you will have to run an infinite loop for this. If you could tell what you are trying to achieve (what is your python file doing), the people here will be able to help better. –  Konstant Jul 28 '11 at 3:57
    
what you need to do is: –  Icarus Jul 28 '11 at 4:33

6 Answers 6

up vote 1 down vote accepted

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

share|improve this answer

From what I understand, you want to keep your process running after close ssh. You could use screen.

share|improve this answer
    
Thank you for this tip! –  tekknolagi Jul 28 '11 at 4:09

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

share|improve this answer

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

share|improve this answer
    
Ideas how I could just let it run once, forever? –  tekknolagi Jul 28 '11 at 3:57
    
What does once forever mean? Only one time? Or one time on a periodic interval forever? –  chubbard Jul 28 '11 at 3:57
    
@tekknolagi: If you put the special time spec @reboot into a cron job line the job will get run once at bootup (if that cron version supports it). There are many ways to do this - this is only one. –  MikeyB Jul 30 '11 at 0:18

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.