Python Forum
how to get PID's of linux commands executed through python modules?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get PID's of linux commands executed through python modules?
#1
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 ?
Reply
#2
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 ?
Reply
#3
Please, don't start new threads. Just bump your original thread after reasonable amount of time. Excessive/frequent bumping is not tolerated.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.

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 ?
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 ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 631 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 639 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,165 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  code not working when executed from flask app ThomasDC 1 834 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  How to see the date of installation of python modules. newbieAuggie2019 4 1,464 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
  Python modules for accessing the configuration of relevant paths Imago 1 1,324 May-07-2022, 07:28 PM
Last Post: Larz60+
  How to use a variable in linux command in python code? ilknurg 2 1,547 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  Sending string commands from Python to a bluetooth device Rovelin 13 9,276 Aug-31-2021, 06:40 PM
Last Post: deanhystad
  Python library for win32 console commands eldiener 3 3,384 Aug-24-2021, 10:28 PM
Last Post: bowlofred
  Python syntax in Linux St0rmcr0w 2 44,723 Jul-29-2021, 01:40 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020