Connecting Tech Pros Worldwide Help | Site Map

How to run python script in background after i logout

  #1  
Old July 24th, 2005, 10:55 AM
Harlin Seritt
Guest
 
Posts: n/a
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

  #2  
Old July 24th, 2005, 10:55 AM
Thanos Tsouanas
Guest
 
Posts: n/a

re: How to run python script in background after i logout


On Sun, Jul 24, 2005 at 02:43:44AM -0700, Harlin Seritt wrote:[color=blue]
> 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,[/color]

This hasn't got to do with python.

It's a unix/linux question.

Check at(1):
man at

--
Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
  #3  
Old July 24th, 2005, 11:05 AM
Thanos Tsouanas
Guest
 
Posts: n/a

re: How to run python script in background after i logout


On Sun, Jul 24, 2005 at 12:51:17PM +0300, Thanos Tsouanas wrote:[color=blue]
> On Sun, Jul 24, 2005 at 02:43:44AM -0700, Harlin Seritt wrote:[color=green]
> > 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,[/color]
>
> This hasn't got to do with python.
>
> It's a unix/linux question.
>
> Check at(1):
> man at[/color]

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.

--
Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
  #4  
Old July 24th, 2005, 12:35 PM
Benji York
Guest
 
Posts: n/a

re: How to run python script in background after i logout


Harlin Seritt wrote:[color=blue]
> 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?[/color]

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.
--
Benji York
  #5  
Old July 24th, 2005, 08:55 PM
Mike Meyer
Guest
 
Posts: n/a

re: How to run python script in background after i logout


Thanos Tsouanas <thanos@sians.org> writes:
[color=blue]
> On Sun, Jul 24, 2005 at 12:51:17PM +0300, Thanos Tsouanas wrote:[color=green]
>> On Sun, Jul 24, 2005 at 02:43:44AM -0700, Harlin Seritt wrote:[color=darkred]
>> > 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:[/color][/color]
> 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.[/color]

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@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
  #6  
Old July 24th, 2005, 09:05 PM
Steve M
Guest
 
Posts: n/a

re: How to run python script in background after i logout


Harlin Seritt wrote:[color=blue]
> 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?[/color]

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.

  #7  
Old July 24th, 2005, 09:15 PM
Marek Kubica
Guest
 
Posts: n/a

re: How to run python script in background after i logout


Hello!

On 24 Jul 2005 12:59:04 -0700 Steve M wrote:
[color=blue]
> 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.)[/color]

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

  #8  
Old July 24th, 2005, 09:55 PM
James David
Guest
 
Posts: n/a

re: How to run python script in background after i logout



"Harlin Seritt" <harlinseritt@yahoo.com> wrote in message
news:1122198224.635804.316950@g49g2000cwa.googlegr oups.com...[color=blue]
> 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?[/color]

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.


  #9  
Old July 25th, 2005, 06:35 AM
Steve Holden
Guest
 
Posts: n/a

re: How to run python script in background after i logout


Harlin Seritt wrote:[color=blue]
> 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
>[/color]
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Closed Thread