Pkill -F Doesn't Work for Process Killing

Kill command does the job but gives error

As @Bodo pointed out, there was more than one PIDs and only one of them got killed while the other showed error as it wasn't started by the same user.

I changed the grep command to filter the process started by the same user and then killed it, so it worked.

Update:

I used this command to successfully run my code and terminate the app as needed.

ps -u | grep service.py | grep -v grep | awk '{print $2}' | xargs kill -9

How to kill all subprocesses of shell?

After starting each child process, you can get its id with

ID=$!

Then you can use the stored PIDs to find and kill all grandchild etc. processes as described here or here.

Bash - starting and killing processes

1,2. Use pgrep. I don't remember off the top of my head whether pgrep has -c parameter, so you might need to pipe that to wc -l.

3: that output is produced by your shell's job control. I think if you run that as a script (not in an interactive shell), there shouldn't be such an output. For an interactive shell, the are number of ways to turn that off, but they are shell-dependent, so refer to your shell's manual.

Kill process by name in ubuntu

You can use killall php. But note that it kills all PHP processes, not just those running script.php.

Bash: Killing all processes in subprocess

You can set a trap in the subshell to kill any active jobs before exiting:

 (trap 'kill $(jobs -p)' EXIT; sleep 5; echo done ) & pid=$!


Related Topics



Leave a reply



Submit