views:

74

answers:

5

As part of an automated test, I have a python script that needs to call two shell scripts that start two different servers that need to interact after the calling script ends. (It's actually a jython script, but I'm not sure that matters at this point.) What can I do to ensure that the servers stay up after the python script ends?

At this point they're called something like this:

 15 def runcmd(str, sleep):
 18    debug('Inside runcmd, executing: ' + str)
 20    os.chdir("/new/dir/")
 22    directory = os.getcwd()
 24    print 'current dir: '+ directory
 25    os.system(str)

 34 t = threading.Thread(
 35   target=runcmd,
 36   args=( cmd, 50,)
 37   )
+1  A: 

os.system() does not return until the process it launches has ended. Use subprocess or Runtime.exec() if you want it in a separate process.

Ignacio Vazquez-Abrams
A: 

I wonder if using subprocess.Popen would work better for you.

maybe doing something like shell=True

M0E-lnx
+2  A: 

Python threads will all die with Python. Also, os.system is blocking. But that's okay -- if the command that os.system() runs launches a new process (but not a child process), all will be fine. On Windows, for instance, if the command begins with "start" the "start"'d process will remain after Python dies.

EDIT: nohup is an equivalent to start on Linux. (Thanks to S. Lott).

Scott Stafford
And for unix you can use an explicit `nohup` to create a process which will linger.
S.Lott
A: 

Threads won't work because they are part of the process. The system call won't work because it blocks as your new process executes.

You will need to use something like os.fork() to spawn a new process and execute it in the new process. Take a look at subprocess for some good cookbook style solutions to this.

0xfe
A: 

Generally, to launch a long-running server that's independent of its parent, you need to daemonize it. Depending on your environment, there are various wrappers that can assist in this process.

Dave Bacher