ronex dicapriyo
ronex dicapriyo

Reputation: 161

Executing and killing a shell process using python script

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).

Upvotes: 0

Views: 5175

Answers (1)

drdaeman
drdaeman

Reputation: 11471

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.

Upvotes: 2

Related Questions