1

Is there a way to call a linux shell commands via Python without having to wait for its completion? I have seen threads to do that in a Windows environment, but not for Linux (Raspbian)...

  • 2
    doesn't [`subprocess.Popen`](https://docs.python.org/2/library/subprocess.html#popen-constructor) do the trick? – mgilson Jan 13 '15 at 22:31

2 Answers2

0

subprocess.Popen should do the trick.

from subprocess import Popen
process = Popen(['sleep', '10'])
print 'Returned immediately!'
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Does it live even after my python application is killed? –  Jan 13 '15 at 22:48
  • @user1688175 -- doubtful. The subprocess is likely created as a child of the python process. When a parent gets killed, the child dies with it. You can however, `atexit.register(process.wait)` -- Now python will wait for that process to complete before the python process exits. – mgilson Jan 13 '15 at 22:50
  • I actually would like to start a C application which monitors the python application. For instance I want to capture if the python application is up... Do you have any idea how to start both applications independently and automatically during the boot? –  Jan 13 '15 at 22:54
  • @user1688175 -- Nope, that's beyond my sys-admin skills... It seems like the two should be started together (e.g. via a shell script) rather than having a python script start a C program that monitors the python script. But I've not ever set up a shell script to execute during system boot -- Although I imagine that it shouldn't be _too_ hard to find a post somewhere on the internet demonstrating how to do that... – mgilson Jan 13 '15 at 22:57
  • FYI: I found it! In the Raspian,you have to edit the /etc/rc.local file and add both commands followed by a " &" before the instruction "exit 0". –  Jan 13 '15 at 23:01
0

You can use subprocess.Popen

import subprocess
proc = subprocess.Popen('echo "test" > a', shell=True)
olofom
  • 6,233
  • 11
  • 37
  • 50