I wish to kill a subprocess when the duration of performing is simply too lengthy.
I understand I must use
However, the issues arrives when should i be not really a root user. For instance, during my designed GUI, I wish to call subprocess, and
os.killpg to get rid of the subprocess. But my GUI is possessed by apache. Then when it involves the command os.kill, I'll get error:
[type: exceptions.OSError value: [Errno 1] Operation not permitted
And besides, the version of my python is 2.4.3. so terminate()...can not be used.
Could anybody produce ideas?
P.S. Related a part of my code:
timeout=4 subp = subprocess.Popen('sudo %s'%commandtosend, shell=True,preexec_fn=os.setsid, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while subp.poll() is None: time.sleep(0.1) now = datetime.datetime.now() if (now - start).seconds > timeout: os.kill(subp.pid, signal.SIGKILL) #os.killpg(subp.pid, signal.SIGKILL) break
Your subprocess is running with superuser rights (because you are beginning it with sudo). To kill it, you have to be superuser.
One option is not to use os.kill but run 'sudo kill 5858' where 5858 will be the PID from the process created by subprocess.Popen.
It is also worth observing when your program enables the consumer to manage commandtosend you'll provide the user superuser privileges towards the entire machine.
sudo in the subprocess command whether it's possible which you want to do since you should not operate a subprocess inside a sudo user out of your GUI , it's certainly a burglar breach:
subprocess.Popen(commandtosend, shell=True,preexec_fn=os ^^ Here don't put sudo
Such as this your subprocess is going to be launch using the www-data user(Apache user), and you will kill it with
If you cannot take away the
sudo (that is bad) in the subprocess you'll have to execute the kill such as this :
os.system("sudo kill %s" % (subp.pid, ))
Hope it will help :)