0

I need to run the 'myexe' executable through python script or python program:

./myexe arg1 arg2 >& outfile

my approach is as follows :

1   import os
2   import subprocess
3   import time
4   cmd = "./myexe arg1 arg2 >& outfile"
5   print 'cmd = ', cmd
6   proc = subprocess.Popen(command, shell=True)
7   # delay for 10 seconds
8   time.sleep(10)                                                       
9   # kill the process if not finished
10  os.system("ps -e | grep \"myexe\" | awk '{print $1}'")               # --- process id = x+1
11  if proc.poll() == None:
14     print 'Deliberately kill the Process= ', proc.pid                 # --- process id = x
15     proc.kill()

Here I noticed that, when I use the io redirection then the process id printed by the system command(line number-10), is different then the one printed in the else part(line number-14).

If I use command as "./myexe arg1 arg2", then the value of process id is correct. So, how can I use subprocess with the shell command which also uses io redirection ?

Here redirection is used for both stderr and stdout in a same file.

Kindly help me in resolving the issue and share efficient approach to accomplish this. And also How can I terminate(kill) the process id-x, if not finished after an arbitrary delay using some efficient approach ?

In addition to this, at last how could I know whether process is terminated succefully or by sending a kill signal: I am doing it as following:

if proc.wait() == 0:
   print 'Terminate successfully'
else:
   print 'Terminated by kill signal'

Or setting some flag after call to proc.kill() can also be used.

Does it adhere with the python rules/ expected usage?

Note: Killing the process is required to avoid the infinite loop scenario which might could occur in myexe executable. outfile is further read by the python script (not shown here).

ronex dicapriyo
  • 161
  • 1
  • 2
  • 12
  • Your `grep` command will match itself. – tripleee Apr 18 '14 at 11:42
  • Doing this in Python is rather pointless if you do all the real work in shell anyway. – tripleee Apr 18 '14 at 11:43
  • @tripleee - what do you mean by grep command will match it self? Here system command prints the correct process id associated with myexe. Value of pro.pid is wrong. I have shown a part of the code which elaborates my problem, It's not an entire code, So it's unfair to deduce that "using python is pointless here" – ronex dicapriyo Apr 18 '14 at 12:04
  • @tripleee, no, because he's using `ps -e`, which only prints basename of the executable. It won't match `1234 pts/1 grep`. – drdaeman Apr 18 '14 at 12:41

1 Answers1

2

Your PIDs differ because of the shell=True argument. Python spawns an intermediate shell process.

The process tree looks like:

python yourscript.py 
└─ /bin/sh -c ./myexe        # PID shown on line 14
   └─ ./myexe                # PID shown on line 10

Replace line 6 with something along those two lines

outfile = open("outfile", "w")
proc = subprocess.Popen(["./myexe", "arg1", "arg2"],
                        stdout=outfile, stderr=outfile)

and your PIDs would match, while both streams would be redirected to a same file (make sure ./myexe flushes those streams).

If you want to use shell, you should refer to this question: How to terminate a python subprocess launched with shell=True.

Community
  • 1
  • 1
drdaeman
  • 11,159
  • 7
  • 59
  • 104
  • Over the link you have mentioned, I found that using exec in command, prints the same process id in both the lines and kill's the correct process, 1) So what about using exec, is it good ?. 2) Using, proc = subprocess.Popen(["./myexe", "arg1", "arg2"], stdout=PIPE, stderr=PIPE) and reading from the PIPE can also be a good option. – ronex dicapriyo Apr 21 '14 at 06:37