473,583 Members | 4,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

running a python script in the background

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("simulat or 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("simulat or 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
Jul 18 '05 #1
7 3209

ar************* *@gmail.com (Arun) wrote:
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

Jul 18 '05 #2
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
On Fri, 29 Oct 2004 23:08:25 -0700, Josiah Carlson <jc******@uci.e du> wrote:

ar************* *@gmail.com (Arun) wrote:
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

--
-AP
Ar************* *@gmail.com
(720)-771-7543
Jul 18 '05 #3
On Sat, 2004-10-30 at 02:42 -0600, Arun Sivakumaran wrote:
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.


http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

Regards,
Cliff

--
Cliff Wells <cl************ @comcast.net>

Jul 18 '05 #4
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
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

Jul 18 '05 #5
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
Jul 18 '05 #6
Pierre Barbier de Reuille <pi************ @cirad.fr> wrote in message news:<41******* *************** *@news.free.fr> ...
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
Jul 18 '05 #7
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("simulat or configfile >> & logfile");

---------------------------------
script.py >> & logfile &

Thanks!
Aun
Jul 18 '05 #8

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

Similar topics

8
3198
by: Eric Brunel | last post by:
Hi all, I was creating a Tkinter widget in the style of the reversed tabs below Excel worksheets and I stepped in a serious problem: the code I made makes python crash with a seg fault, bus error or X11 BadGC error on both Solaris (2.6 and 2.7) and Linux (Mandrake 8.0); it doesn't crash on Windows. I tried to simplify the script, but I...
3
1431
by: Alfred | last post by:
hi all, i'm trying to figure out if there is some easy way to run a python script in the background _without_ specifying "&" in a linux enivornment. script.py & vs
9
1973
by: Erik Geiger | last post by:
Hi, sorry, my english ist not that got but I'll try. I have a running python script (capisuit incoming.py). This script shall start a linux shell script. If I start this script like os.system(/paht/to shellscipt.sh) the python scipt waits for the exit of the shell script and then goes on with the rest of the python script. How to start...
7
1539
by: black | last post by:
hi all~ in my .py file there are a few print to trace out some message and i wonder if we can save it into a specified file when that script get running. if so, i may just check that file to c how the script is running. can anyone show me a right direction ? one million tks~
8
3024
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to what I really should do. I've had a look through Programming Python and the Python Cookbook, which have given me ideas, but nothing has gelled...
13
5991
by: wattersmt | last post by:
Hello, I am trying to write a python cgi that calls a script over ssh, the problem is the script takes a very long time to execute so Apache makes the CGI time out and I never see any output. The script is set to print a progress report to stdout every 3 seconds but I never see any output until the child process is killed. Here's what I...
5
3474
by: This | last post by:
I have a pretty basic emailing script that sends a relatively small number (150) of html emails. The emails are compiled, personalised from a mysql db subscribers list, and sent using mail() - after sending, a small summary html page is sent to the user with number sent, time taken and a simple navigation choice. Up to about 100 emails it...
3
2421
by: sophie_newbie | last post by:
Hi, I have a cgi script that performs a very long computation that can take several hours to complete. Is there any smart way that I can keep this script running until it is finished (after the user has closed the browser) and email them with the results. The email bit isn't the problem, I just don't know how to keep the code running in the...
6
2707
by: Guillermo | last post by:
Hi, I need a script to keep running in the background after it's loaded some data. It will make this data available to the main program in the form of a dictionary, but I don't want to reload the calculated data every time the user needs it via the main program. I won't be working with an UI, hope that can be made easily in Python...
0
7894
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7825
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8191
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5700
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1431
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1155
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.