1

The simple python3 program named print-pid.py is running :

cat print-pid.py
import os 
from time import sleep  
print('my pid is',os.getpid())
sleep(1000)

The output for python3 print-pid.py is :

my pid is 5421

To get the pid with bash command.

ps aux|grep 'python3 print-pid.py'|grep -v grep |awk '{print $2}'
5421

I wan to get the pid of the program python3 print-pid.py when it is running with python's sbuprocess module.

It is my try here:

import subprocess
cmd = "ps aux|grep 'python3 print-pid.py'|grep -v grep |awk '{print $2}'"
result = subprocess.run(cmd, stdout=subprocess.PIPE,shell=True)
print(result)

The result confued me.

CompletedProcess(args="ps aux|grep 'python3 print-pid.py'|grep -v grep |awk '{print $2}'", returncode=0, stdout=b''

The eturncode=0 means my bash command execute successfully,why stdout=b'' instead of stdout=b'5421'?

Thank for Allan's replace and test suggestion in the comment.

For the bash command ps aux|grep 'python3' :

import subprocess
cmd = "ps aux|grep 'python3'"
result = subprocess.run(cmd, stdout=subprocess.PIPE,shell=True)
print(result)

the output is:

CompletedProcess(args="ps aux|grep 'python3'", returncode=0,     
stdout=b'debian9   6569  0.0  0.2  31916  9376 pts/0    S+   11:04   0:00 python3 print-p\ndebian9   6844  0.0  0.2  39860 11440 pts/2    S+   11:13   0:00 python3\ndebian9   6929  0.0  0.0  16980   948 pts/2    S+   11:17   0:00 grep python3\n') 

However for the bash command ps aux|grep 'python3 print-pid.py':

import subprocess
cmd = "ps aux|grep 'python3 print-pid.py'"
result = subprocess.run(cmd, stdout=subprocess.PIPE,shell=True)
print(result)

the output:

CompletedProcess(args="ps aux|grep 'python3 print-pid.py'", returncode=1, stdout=b'')

Why isn't it printing?

stdout=b'' instead of  stdout=b'debian9   6569  0.0  0.2  31916  9376 pts/0    S+   11:04   0:00 python3 print-p\n
debian9   6929  0.0  0.0  16980   948 pts/2    S+   11:17   0:00 grep python3 print-pid.py\n'
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • 4
    Your return code is going to be 0 whether or not anything is found; pipeline exit status is that of the final command in the pipeline, and `awk` doesn't think anything went wrong whether or not it gets input (it printed all the `$2` fields it found for each line; not a problem if there are no lines). Are you sure your other program is still running? Maybe make it sleep a bit longer to ensure it sticks around long enough to see it? – ShadowRanger Jan 24 '18 at 03:03
  • @ ShadowRanger ,my other program is still running,please have a try. –  Jan 24 '18 at 03:06
  • Could your replace your `cmd` by `cmd = "ps aux|grep 'python3 print-pid.py'|grep -v grep` and tell me what is your output without the `awk` – Allan Jan 24 '18 at 03:07
  • cmd = "ps aux|grep 'python3'" can get right output, instead of cmd = "ps aux|grep 'python3 print-pid.py'" ,why? –  Jan 24 '18 at 03:16
  • 3
    If you cut off the rest of the pipeline after the `grep 'python3'` and inspect the output to see why it doesn't match `python3 print-pid.py`, you should be able to figure that out. – Charles Duffy Jan 24 '18 at 03:27
  • Is this some kind of rube goldberg machine? – wim Jan 24 '18 at 03:29
  • 1
    (btw, consider my admonishments wrt. `ps aux | grep` being an antipattern to have been reiterated; see [ProcessManagement](https://mywiki.wooledge.org/ProcessManagement) for a detailed discussion of best practices -- particularly the section "How to Work With Processes", through and inclusive of the subsection at the end "On processes, environments and inheritance"). – Charles Duffy Jan 24 '18 at 03:30
  • I would also suggest reading this link: https://stackoverflow.com/questions/13332268/python-subprocess-command-with-pipe – Allan Jan 24 '18 at 03:30

0 Answers0