Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing a script launched in a Process via os.system()

I have a python script which launches several processes. Each process basically just calls a shell script:

from multiprocessing import Process
import os
import logging

def thread_method(n = 4):
    global logger
    command = "~/Scripts/run.sh " + str(n) + " >> /var/log/mylog.log"
    if (debug): logger.debug(command)
    os.system(command)

I launch several of these threads, which are meant to run in the background. I want to have a timeout on these threads, such that if it exceeds the timeout, they are killed:

t = []
for x in range(10):
    try:
        t.append(Process(target=thread_method, args=(x,) ) )
        t[-1].start()
    except Exception as e:
        logger.error("Error: unable to start thread")
        logger.error("Error message: " + str(e))
logger.info("Waiting up to 60 seconds to allow threads to finish")
t[0].join(60)
for n in range(len(t)):
    if t[n].is_alive():
    logger.info(str(n) + " is still alive after 60 seconds, forcibly terminating")
     t[n].terminate()

The problem is that calling terminate() on the process threads isn't killing the launched run.sh script - it continues running in the background until I either force kill it from the command line, or it finishes internally. Is there a way to have terminate also kill the subshell created by os.system()?

like image 585
L.J. Avatar asked Sep 07 '12 15:09

L.J.


People also ask

What does OS kill do in Python?

kill() method in Python is used to send specified signal to the process with specified process id. Constants for the specific signals available on the host platform are defined in the signal module.

How do you kill a process in Python?

A process can be killed by calling the Process. kill() function. The call will only terminate the target process, not child processes. The method is called on the multiprocessing.

How do you kill PID in Python?

You can kill a process via its pid with the os. kill() function. The os. kill function takes two arguments, the process identifier (pid) to kill, and the signal to send to kill the process.

How do you kill a terminal in Python?

To stop a script in Python, press Ctrl + C. If you are using Mac, press Ctrl + C. If you want to pause the process and put it in the background, press Ctrl + Z (at least on Linux). Then, if you want to kill it, run kill %n where “n” is the number you got next to “Stopped” when you pressed Ctrl + Z.


3 Answers

You should use an event to signal the worker to terminate, run the subprocess with subprocess module, then terminate it with Popen.terminate(). Calling Process.terminate() will not allow it worker to clean up. See the documentation for Process.terminate().

like image 171
quantum Avatar answered Oct 18 '22 02:10

quantum


Use subprocess instead, whose objects have a terminate() method explicitly for this.

like image 2
Ignacio Vazquez-Abrams Avatar answered Oct 18 '22 02:10

Ignacio Vazquez-Abrams


In Python 3.3, the subprocess module supports a timeout: http://docs.python.org/dev/library/subprocess.html

For other solutions regarding Python 2.x, please have a look in this thread: Using module 'subprocess' with timeout

like image 2
Dr. Jan-Philip Gehrcke Avatar answered Oct 18 '22 01:10

Dr. Jan-Philip Gehrcke