1

I use the following snippet in a larger Python program to spawn a process in background:

import subprocess

command = "/media/sf_SharedDir/FOOBAR"
subprocess.Popen(command, shell=True)

After that I wanted to check whether the process was running when my Python program returned. Output of ps -ef | grep -v grep | grep FOOBAR:

ap    3396   937  0 16:08 pts/16   00:00:00 /bin/sh -c /media/sf_SharedDir/FOOBAR
ap    3397  3396  0 16:08 pts/16   00:00:00 /bin/sh /media/sf_SharedDir/FOOBAR

I was surprised to see two lines of and they have differend PIDs so are those two processes running? Is there something wrong with my Popen call?

FOOBAR Script:

#!/bin/bash

while : 
do
        echo "still alive"
        sleep 1
done

EDIT: Starting the script in a terminal ps displayes only one process.

Started via ./FOOBAR

ap@VBU:/media/sf_SharedDir$ ps -ef | grep -v grep | grep FOOBAR
ap    4115  3463  0 16:34 pts/5    00:00:00 /bin/bash ./FOOBAR

EDIT: shell=True is causing this issue (if it is one). But how would I fix that if I required shell to be True to run bash commands?

ap0
  • 1,083
  • 1
  • 12
  • 37
  • 2
    I see no output and only one process, I did chmod +x the file and run without shell=True – Padraic Cunningham Dec 01 '15 at 15:30
  • First time I though I had one process still running and forgot about that but after killing them all and rerunning the python script I saw two processes again. – ap0 Dec 01 '15 at 15:32
  • @PadraicCunningham you are right. `shell=True` is causing this. But how would I fix this if I require it to be `True`? – ap0 Dec 01 '15 at 15:38
  • 1
    `subprocess.Popen(command)`, just make the script executable and `./`, why would you need shell=True? – Padraic Cunningham Dec 01 '15 at 15:40
  • Because in my programm I need to run shell commands with options and arguments. It is not only a single file I try to run. And in this case - if I understood correctly - I need the `shell=True` flag. For example I also run `ps -ef | grep -v grep | grep ...` through `Popen`. – ap0 Dec 01 '15 at 15:44
  • you can pipe with Popen, http://stackoverflow.com/a/29085292/2141635 http://stackoverflow.com/questions/31710529/error-is-being-raised-when-executing-a-sub-process-using/31710577#31710577 – Padraic Cunningham Dec 01 '15 at 15:45

1 Answers1

1

There is nothing wrong, what you see is perfectly normal. There is no "fix".

Each of your processes has a distinct function. The top-level process is running the python interpreter.

The second process, /bin/sh -c /media/sf_SharedDir/FOOBAR' is the shell that interprets the cmd line (because you want | or * or $HOME to be interpreted, you specified shell=True).

The third process, /bin/sh /media/sf_SharedDir/FOOBAR is the FOOBAR cmd. The /bin/sh comes from the #! line inside your FOOBAR program. If it were a C program, you'd just see /media/sf_SharedDir/FOOBAR here. If it were a python program, you'd see /usr/bin/python/media/sf_SharedDir/FOOBAR.

If you are really bothered by the second process, you could modify your python program like so:

command = "exec /media/sf_SharedDir/FOOBAR"
subprocess.Popen(command, shell=True)
Robᵩ
  • 163,533
  • 20
  • 239
  • 308