running a python script in the background

A

Arun

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 the system() function to run the simulator) over a list of
config files. I want the python script to itself run as well as start
the simulations totally in the background (the simulations run for a
day or two, so basically I want to start them in background and log
off).

The structure of script is somewhat like this:
--------------------------------

Create the config files

Loop through the list of config files:
system("simulator configfile");

---------------------------------

Now if I start the script in the background with "script.py &", it
doesn't work. I figure the reason is the system() call spawns the
simulator in the foreground.

If I change the system call to system("simulator configfile &"), then
the script will spawn off the simulator with the config files all at
once.

Is there a way I can make this all run in the background, while at the
same time ensuring that only one instance of the simulator is running?

Thanks!,
Arun
 
J

Josiah Carlson

the simulations totally in the background (the simulations run for a
day or two, so basically I want to start them in background and log
off).
Now if I start the script in the background with "script.py &", it
doesn't work. I figure the reason is the system() call spawns the
simulator in the foreground.
Is there a way I can make this all run in the background, while at the
same time ensuring that only one instance of the simulator is running?

It sounds like you are running this on *nix. The Python interpreter is
likely dying when you log-off due to a 'hangup' signal being called when
you log out.

What has worked for me in the past is to SSH to localhost, executing:
"nohup python script.py &" and disconnecting. It should work running
from a local terminal (without SSH), but I can't guarantee it.

- Josiah
 
A

Arun Sivakumaran

Actually, I am working from a 32-bit linux client and am SSHing to a
64-bit itanium server (not sure what's being run there).

I had tried the nohup option too before, that doesn't seem to help.

Thanks,
Arun
 
D

Dirkjan Ochtman

I've found in the past that it might work better when you redirect
stdout and stderr, since errors will arise when the Python script
suddenly doesn't have either anymore because your session ended.

python script.py >& /dev/null &

HTH,

Dirkjan
 
P

Pierre Barbier de Reuille

I'd use "screen". This is a command that creates shells you can
disconnect/reconnect.

So you launch screen and launch your program inside it. Then, you can
disconnect from the screen and log out. When you'll log in, you'll be
able to reconnect to your screen and see the result of your program.

Pierre
 
A

Arun

Pierre Barbier de Reuille said:
I'd use "screen". This is a command that creates shells you can
disconnect/reconnect.

So you launch screen and launch your program inside it. Then, you can
disconnect from the screen and log out. When you'll log in, you'll be
able to reconnect to your screen and see the result of your program.

Pierre

Hi all,

Thanks for those replies. I'll try out the solutions suggested.

-Arun
 
A

Arun

The reason I wasn't able to run the script in the background was
because my simulator was outputting debug messages in the foreground.
The following seemed to do the trick:

script.py
--------------------------------

Create the config files

Loop through the list of config files:
system("simulator configfile >> & logfile");
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top