4
votes

I want to create a child process which will run after the parent has finished.

I have tried:

subprocess.Popen(['myscript'])
exit()

The child is created, but the parent waits until the child has finished. Instead, I want to finish quickly and let the child do the hard work in the background. Any suggestions?

Some clarifications:

  • it is running on Linux (Ububntu Server 14.04)
  • python 2.7 preferably
  • I don't want to communicate with the child in any way. The child is totally independent process.

I need the main process to finish quickly as it will be accessed from the web. It is meant to trigger another script. The result will be sent to email later on (by the child process). So, no need for the user to wait for the answer. Just quick "ok" and then the child starts.

EDIT: Okay. Now to be honest, even my example in this post works in the simplest case (just those 2 lines + script which sleeps for 10 seconds + writes a file => main finishes and I get a file in 10 seconds). The actual situation is somewhat more complicated.

There is one script, which iterates over a list of scripts which all have to be executed. This main script uses subprocess.Popen with communicate() (waits for the output). This calls my "main" script which I'm asking help here. This "main" does something and then wants to run a child in background. In that context, the "main" waits for the child.

I will try to make a short cleaned up code example.

4
Try subprocess.Popen(['myscript'], start_new_session=True)multivac
@J.F.Sebastian Daemon might be a bit of an overkill here. Sadly, Ago doesn't mention if the spawned process has to be further controlled somehow by the user (stop, pause, pipe input/output etc.). If no control is required and the spawned process contains code that does its thing and then automatically goes to a non-existent state, you need not do something as complex as creating a daemon. The OP should provide this information so that we can evaluate what is the optimal solution in his case.rbaleksandar
Should mention this in the comment and not just type "related". Posting links without giving information about their content and how it's related to the question at hand is not a good thing to do.rbaleksandar

4 Answers

1
votes

I think this will work:

import os  
os.spawnl(os.P_NOWAIT, 'myscript')

If you only want to run the child process on Linux, Here is a another way using GNU program nohup like this:

subprocess.Popen(['nohup '+'myscript'+' &'])
# or use os.system()
0
votes

If you use Linux, then one way to do that is to use nohup. Note that nohup will also redirect the standard output to nohup.out which might be undesirable

subprocess.Popen(['nohup', 'myscript'])
exit()
0
votes

You can try to use system() (documentation). Something like :

import os
os.system('myscript' + ' &')

The & at the end of a command will execute it in the background in a subshell.

0
votes

use the following snippet which will work

import subprocess, os
subprocess.Popen(['myscript'])
os._exit(0)