473,594 Members | 2,694 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 3210

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 couldn't reproduce the crash with a simpler code. So the code below is somewhat long; sorry for that. ...
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 a shell script without waiting for the exit of that shell
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
3025
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 yet, so I thought I'd put the question to the community. But first, let me be a little more detailed...
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 have in my python script:
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 all works fine - this takes the server about 27 secs . Any more than that and although the emails are...
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 background. I'm sure there is a smart way to do this... Thanks!
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 somehow.
0
7954
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7882
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8259
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8016
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5836
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5415
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3871
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3905
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.