How to run python script in background after i logout

H

Harlin Seritt

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:

python script.py &

It runs fine. When I log off ssh I notice that the script died when I
logged off. How do I make sure it stays running?

thanks,

Harlin Seritt
 
T

Thanos Tsouanas

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:

python script.py &

It runs fine. When I log off ssh I notice that the script died when I
logged off. How do I make sure it stays running?

thanks,

This hasn't got to do with python.

It's a unix/linux question.

Check at(1):
man at
 
T

Thanos Tsouanas

This hasn't got to do with python.

It's a unix/linux question.

Check at(1):
man at

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.
 
B

Benji York

Harlin said:
python script.py &

It runs fine. When I log off ssh I notice that the script died when I
logged off. How do I make sure it stays running?

As another reply stated, cron is probably what you really want, but to
answer your question literally: you want the "nohup" command (short for
"no hangup", as in the HUP signal). You would run it like so:

nohup python script.py &

see "man nohup" and "info coreutils nohup" for more info.
 
M

Mike Meyer

Thanos Tsouanas said:
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
 
S

Steve M

Harlin said:
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:

python script.py &

It runs fine. When I log off ssh I notice that the script died when I
logged off. How do I make sure it stays running?

You might also check out the extremely cool screen program. It lets you
have multiple virtual terminal sessions from one connection, and detach
them all and logout, then login later and re-attach them. I typically
use 'screen -D -R' which will re-attach if there is a set of sessions
to re-attach, or otherwise start a new one.

Then you can have a dedicated window for your script.py (you don't even
need to run it in the background of the shell with '&') and you can
just detach the screen before you logout. Later you can log back in,
reattach, check for any output (you can use print statements for debug
info, etc.).

Since it can be tricky getting started, I'll tell you briefly, there is
a command key, which you use to send commands to the screen program.
Anything other than command key will be passed through to whatever
program is running, e.g. the bash shell or whatever. The default
command key is ctrl-a. So you would do 'ctrl-a c' to create a new
virtual window, 'ctrl-a 1', 'ctrl-a 2', etc. to switch between virtual
windows, and 'ctrl-a d' to detach your session. This brings you back to
your original ssh login shell. Incidentally, if you do a 'ps aux' here
you'll see one of the programs is 'SCREEN' owned by root; this is the
process that is keeping alive all your other processes and that
persists when you logout and allows you to reattach later.

A couple of problems I've had are first, that ctrl-a is also the emacs
command to go to the beginning of the line, which I use all the time.
So I've sometimes rebound the screen command key (I've tried ctrl-[
since I dont' ever seem to use that for anything else, but I don't
think it works entirely perfectly, especially in combination with the
next problem.). Another is that when I use putty.exe from Windows for
my ssh client, I can't get scroll-back buffers to work correctly with
screen. (Screen is really powerful with its own scrollback buffers and
screendumps and stuff but I don't have time to get into all that or
even learn it sometimes. I wish I were more a master of it since its
such a great program.)

Another alternative is to daemonize your program, but I don't know how
to do that off the top of my head.
 
M

Marek Kubica

Hello!

Another is that when I use putty.exe from Windows for
my ssh client, I can't get scroll-back buffers to work correctly with
screen. (Screen is really powerful with its own scrollback buffers and
screendumps and stuff but I don't have time to get into all that or
even learn it sometimes. I wish I were more a master of it since its
such a great program.)

I use Crtl+A+Esc and go into the screen scrollback buffer, where I can
navigate with the arrow keys. After typing Esc a second time, I get back to
my program.

greets,
Marek
 
J

James David

Harlin Seritt said:
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:

python script.py &

It runs fine. When I log off ssh I notice that the script died when I
logged off. How do I make sure it stays running?

It sounds like you want to run your script as a daemon.

There are ways to do this and it is more of a *nix issue, but google
"python" and "run as a daemon". Also check out the createDaemon() function.
 
S

Steve Holden

Harlin said:
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:

python script.py &

It runs fine. When I log off ssh I notice that the script died when I
logged off. How do I make sure it stays running?

thanks,

Harlin Seritt
If you want to trigger each run manually, try

nohup python script.py &

This should allow the job to continue running after you've logged out.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top