Forums

How to kill a long-running process

I've used the socket technique for long-running processes described in the Help section, and while my process does not seem to be doing its job any longer, I don't see a way to kill it (after which the scheduler would take care o restarting it).

I've seen a number of questions about killing processes, some quite old, basically saying some combination of "you can't" and "we're working on it", so I figured I would ask again and see what the current situation was.

We've sorted out the problems with killing the processes; if you're launching it from a scheduled task then you just need to go to the "Schedule" tab, then hit the small "Reload" icon to the top right of the process list at the bottom of the page to see your running processes. Then there should be buttons to kill each of them.

(The counter-intuitive need to click the "Reload" icon to see your processes in the first place is a UI bug, we've got a fix for that going through our testing system.)

Hi @Giles @Harry et al.

I have an issue where periodically my tasks that should only run for about 10 minutes seem to hang up and run for hours. They eat up my CPU time and force me into the tarpit (argh!). I'd trying to figure out a way to kill these long running processes automatically. I'm thinking maybe schedule a task that will look for processes that have been running for more than X amount of time and then kill them.

Is this doable?

Hmm, unfortunately not :-( An API for listing and killing processes is something we're thinking of adding -- the basic infrastructure is there -- but we don't have a timeline for it yet. I'll add an upvote on your behalf, though!

I was able to solve this by using the subprocess module in a separate .py file. Basically, I have a new .py file that acts as the manager for the task I really want to run:

import subprocess 
try:
    proc = subprocess.run(["python", "generate_events.py"], stdout=subprocess.PIPE, timeout=60*15)
    print(proc.stdout.decode('ascii'))
except subprocess.TimeoutExpired:
    print('Process ran too long and was killed.')

Then I call the above .py from the Task management UI. If the task runs for longer than 15 minutes, it will raise the timeout exception and automatically quit. I need to add some logging about what the subprocess actually did while it ran before it timed out, but for now this is enough.

NOTE: I actually had to modify the paths of the two args input into the subprocess to match my former inputs into the Task management UI. Since I was running the original task from a virtualenv and I was running a .py file from deep within my directory structure, I had to modify the path on both the python call and the script so the subprocess could find them both.

That's a great solution! Thanks for sharing it.