473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to run python script in background after i logout

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

Jul 24 '05 #1
8 38087
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:

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

--
Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
Jul 24 '05 #2
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:

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


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/
Jul 24 '05 #3
Harlin Seritt wrote:
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.
--
Benji York
Jul 24 '05 #4
Thanos Tsouanas <th****@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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 24 '05 #5
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:

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.

Jul 24 '05 #6
Hello!

On 24 Jul 2005 12:59:04 -0700 Steve M wrote:
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

Jul 24 '05 #7

"Harlin Seritt" <ha**********@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.
Jul 24 '05 #8
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:

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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Jul 25 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Andrew Chalk | last post by:
Is this possible? In my CGI app. I display a web page with a link (anchor). When the link is clicked I want to exectute a python script rather than go to an HTML page. Many thanks.
7
by: Arun | last post by:
Hi, This is a scripting question, but since I am writing the script in python I am posting this question here: I have a python script that runs a simulator (that was written in c++, so I use...
8
by: Jan Gregor | last post by:
Hello I run python script on another computer and want to "survive" that script after my logout. the script also uses drive mapping to network drive. Can you help me ? Or better is there some...
6
by: tatamata | last post by:
Hello. How can I run some Python script within C# program? Thanks, Zlatko
3
by: m.errami | last post by:
Hello all. I know the question will seem stupid but googling it gives me only stuff I don't care about. So the question is the following: When I start a python script containing a wxApp under...
1
by: michael.buonomo | last post by:
We have been using the Google recommended python script for about a year. We recently realized that the script was not crawling our sites url's, but just our folders which reside on the server. The...
4
by: Quill_Patricia | last post by:
I have a Python script which is used to load data into a database. Up to now this script has been run by customers from the Windows command prompt using "python edg_loader.pyc". Any error messages...
3
by: Gros Bedo | last post by:
Hello :-) I have a question about Python and Linux shell. I have a python program which is permanently resident in the end-user system. I'm currently producing a RPM package, and it works nicely....
1
by: Gros Bedo | last post by:
Yes I've seen that each python script calls its own instance of Python. Buthow to know which is the good one in bash ? Is there a command that gets the parameters of process, so I could use grep to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.