0

How can I terminate a process that is launched from within an application programmatically? To clarify, there is an application that launches a WebKit subprocess from time to time, and I want to kill this subprocess shortly after it begins (i.e., not immediately, but after 3 seconds or so).

What I currently do is grep the output of ps aux for WebKit and then either kill with the PID or killall the process binary, but this seems like something I should be able to automate. If I know the subprocess gets launched every 5 minutes, for example, what tools are available for to keep something running in the background to check for new instances of this subprocess and programmatically kill it? (BTW, pkill and pgrep are not available to me.)

  • ps aux | grep, wait 3 seconds, killall, loop endlessly? Do you control the parent (as in, you can modify the code for that)? – muru Jan 20 at 8:36
  • @muru that's more or less what i'm already doing. but i imagined a sort of listener that might constantly monitor the process list on my behalf. i'm unable to modify the parent's code. – grad student Jan 20 at 8:52
  • You're not telling what system you're using: On Linux, proc_connector/forkstat is another lightweight way to monitor forks and execs which doesn't involve reading the whole process list. It only works as root, though. And even if you're not able to modify the source code, you can override library functions (eg. exec*()) via a LD_PRELOAD hack. More details are needed for any non-guessing answer. – mosvy Jan 20 at 13:20
  • @mosvy it's an apple os, as you probably suspected. I have compiled some linux tools for use on it, but the proc related stuff isn't going to fly. Is there a simple way to using near-universal unix tools to script touching a file when the subprocess fires? – grad student Jan 20 at 17:26
  • pgrep and pkill are available on macOS via homebrew – muru Jan 21 at 4:08
0

The trick is to make sure you are killing the right process and only the ones that you want. This sounds simple, but so many people stuff it up. The worst way programatically is to do it by name.

OK, so you said you cannot modify the parent. The right way if you could do that is to take the return value of the fork() call as it has the child PID. The PID is unique while the name (which is what I think you are doing with ps aux | grep) is not unique.

Assuming you know the PID of the parent, pgrep -P might be one way to do it. This assumes you have a single child process here as that will match all of the parent's child processes.

Oddly enough, killall matches on age, pgrep/pkill matches on parent, but they don't do what the other does; something to fix one day perhaps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.