Jan-24-2018, 02:48 AM
The simple python3 program named print-pid.py is running :
my pid is 5481
To get the pid with bash command(open a new terminal to run).
It is my try here:
The returncode is 0 means the bash cmd executed well in subprocess,why stdout is not stdout=b'5481' ?
import os from time import sleep print('my pid is',os.getpid()) sleep(1000)The result is :
my pid is 5481
To get the pid with bash command(open a new terminal to run).
#bash code ps aux|grep 'python3 print-pid.py'|grep -v grep |awk '{print $2}' 5481I want 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)CompletedProcess(args="ps aux|grep 'python3 print-pid.py'|grep -v grep |awk '{print $2}'", returncode=0, stdout=b''
The returncode is 0 means the bash cmd executed well in subprocess,why stdout is not stdout=b'5481' ?