0

Within a python program I need to run a command in background, without displaying its output. Therefore I'm doing os.system("nohup " + command + " &") for now.

Edit : command shouldn't be killed/closed when python program exits.

However that will only work on Linux, and the content of the file will end up in nohup.out but I don't need it there. Therefore I'm looking for a platform independent solution. os.spawnlp(os.P_DETACH, command) doesn't work, even with the *p version so as to be able not to enter full path to application.

NB. I know that command is generally platform dependent, but that's not the point of my question.

1

2 Answers 2

3

You are looking for a daemon process. Look at How do you create a daemon in Python? or http://blog.ianbicking.org/daemon-best-practices.html

Sign up to request clarification or add additional context in comments.

1 Comment

3

Look into the subprocess module.

from subprocess import Popen, PIPE process = Popen(['command', 'arg'], stdout=PIPE) 

3 Comments

Works fine. But command is killed when python program closes.
You'll have to wait for it to finish. You can use process.communicate() or poll on the return code.
command has to be independent from python script. If I kill python, command has to keep running.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.