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.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
pictuga
  • 143
  • 2
  • 8
  • possible duplicate of [How do you create a daemon in Python?](http://stackoverflow.com/questions/473620/how-do-you-create-a-daemon-in-python) – Aaron Digulla Aug 22 '11 at 15:43

2 Answers2

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

Community
  • 1
  • 1
rocksportrocker
  • 7,251
  • 2
  • 31
  • 48
  • http://stackoverflow.com/questions/972362/spawning-process-from-python/972383#972383 looks good – pictuga Aug 28 '11 at 14:46
2

Look into the subprocess module.

from subprocess import Popen, PIPE
process = Popen(['command', 'arg'], stdout=PIPE)
nmichaels
  • 49,466
  • 12
  • 107
  • 135