how to get PID's of linux commands executed through python modules? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: how to get PID's of linux commands executed through python modules? (/thread-24933.html) |
how to get PID's of linux commands executed through python modules? - Manikandan_PS - Mar-11-2020 1]i am using python 2.6.6 2]i need to execute a linux command { tail -f file1 -f file2 | egrep --line-buffered "string1|string2" > tofile.txt &} in background. 3]when executing the command on a linux shell, i could see the pid's as "[1] 2740 2741" but, i am not able to read this output through subprocess.Popen, os.popen and commands.getstatusoutput. commands give error as "syntax error near unexpected token '&' and others give empty string as output and command is running the background successfully though. 4]subprocess.Popen gives a pid but when killed or terminated i could still see the tail and egrep pid's staying alive. 5]how to kill/terminate all these processes ? how to get PID's of linux commands executed through python modules? - Manikandan_PS - Mar-12-2020 1]i am using python 2.6.6 2]i need to execute a linux command { tail -f file1 -f file2 | egrep --line-buffered "string1|string2" > tofile.txt &} in background. 3]when executing the command on a linux shell, i could see the pid's as "[1] 2740 2741" but, i am not able to read this output through subprocess.Popen, os.popen and commands.getstatusoutput. commands give error as "syntax error near unexpected token '&' and others give empty string as output and command is running the background successfully though. 4]subprocess.Popen gives a pid but when killed or terminated i could still see the tail and egrep pid's staying alive. 5]how to kill/terminate all these processes ? RE: how to get PID's of linux commands executed through python modules? - buran - Mar-12-2020 Please, don't start new threads. Just bump your original thread after reasonable amount of time. Excessive/frequent bumping is not tolerated. RE: how to get PID's of linux commands executed through python modules? - buran - Mar-12-2020 (Mar-12-2020, 07:03 AM)Manikandan_PS Wrote: i am not able to read this output through subprocess.Popen, os.popen and commands.getstatusoutput. commands give error as "syntax error near unexpected token '&' and others give empty string as output and command is running the background successfully though.Maybe show your code... (Mar-12-2020, 07:03 AM)Manikandan_PS Wrote: subprocess.Popen gives a pid but when killed or terminated i could still see the tail and egrep pid's staying alive.Do you pass shell=True ? in which case as docs state:Quote:Note that if you set the shell argument to True, this is the process ID of the spawned shell. RE: how to get PID's of linux commands executed through python modules? - Manikandan_PS - Mar-12-2020 understood...thought of moving the question to the appropriate group and surely not to repeat the question again. yes i passed shell=True and i get 3 consecutive PID's, 1 for shell and the others for tail and egrep as seen through command "ps -ae" but, when shell pid is killed the other two still remains. i am just trying these commands over python shell to see the behavior and then finally to use it in my script: ###############from python shell environment############ import os import subprocess command_1 = "tail -f file1*.log -f file2* -f file3* | egrep --line-buffered "string1:|string2" > /export/home/output.txt &" p = subprocess.Popen(command_1,stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True) (out,error) = p.communicate() ###############from python shell environment############ 1] p.pid says 22559 and 22560 for tail and 22561 for egrep as seen through linux command <ps -ae> 2] upon calling p.terminate(), i don't see pid 22559 but the other pid's 22560 and 22561 still remains. how to terminate all these pids ? (Mar-12-2020, 07:16 AM)Manikandan_PS Wrote: understood...thought of moving the question to the appropriate group and surely not to repeat the question again.both out and error were returning empty but when i execute this command on a linux shell environment i get the pids for both tail and egrep. how to get these two pid's in python ? |