How to run python script in background after i logout

Mike Meyer mwm at mired.org
Sun Jul 24 15:48:35 EDT 2005


Thanos Tsouanas <thanos at sians.org> writes:

> On Sun, Jul 24, 2005 at 12:51:17PM +0300, Thanos Tsouanas wrote:
>> On Sun, Jul 24, 2005 at 02:43:44AM -0700, Harlin Seritt wrote:
>> > I have a remote linux server where I can only access it via ssh. I have
>> > a script that I need to have run all the time. I run like so:
> Since you want it to run all the time, check cron(8) as well.
> Maybe you would like to write a small script, executing script.py if it
> is not already running.

Cron will restart the process at regular intervals, meaning you have
to have a mechanism in place to deal with multiple copies running. It
also allows for periods when no process is running.

There are usually better solutions than this, but they are
system-dependent.

On SysV like systems - which includes most (all?) Linux systems - you
can add an entry to inittab that will cause a command to be restarted
should it ever die.

On BSD based systems, the same functionality is available via
/etc/ttys. It's not very well documented, though.

The reason you don't hear much about these is because it's normal for
a Unix app to be able to deal with such issues by itself. In python,
this consists of a loop like:

    while not_time_to_exit:
        try:
            run_main()
        except:
            log_problem()

I.e. - you catch any otherwise uncaught exceptions, and log them so
that you can fix whatever caused the problem later. This requires that
you run_main resets the environment properly to avoid problems from
leftover data in the python program. Of course, one way of doing that
is relaunching the python program.

not_time_to_exit depends on your environment. Proper signal handling
is usually the way to deal with this.

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list