Categories: Linux

One Line Linux Command to Kill Parent Process and Spawned Children in A Group

In Linux, if you’d like to kill a command and all processes spawned by that command do this (in our example the command “parent” is example.py):

 
 

kill -- -$(ps -ae o pid,pgrp,cmd | grep "[e]xample.py" | awk '{print $2}' | tail -1)

 

This will kill the “example.py” process and anything that it spawned.

How it works:

To kill all processes of a group the command is

 
kill -- -groupid
 

To get the group id you do the ps command using the arguments as shown, grep it for your command, but formatting example.py with quotes and using the bracket for the first letter (this filters out the grep command itself) then filter it through awk to get the second field which is the group id. The tail -1 gets rid of duplicate group ids. Yout put all of that in a variable using the $() syntax and voila – you get the group id. So you substitute that $(mess) for that -groupid in the kill command above.

Charlie

Recent Posts

KeePassXC Password Manager – Use Auto-Type Instead of the KeePassXC Plugin

Avoiding Browser Plugins with KeePassXC KeePassXC is a popular free open source password manager. As…

3 years ago

A Simple Step to Delete Thousands of Useless Gmail Messages and Free Up Space

If you've had a Gmail account for years, like I have, at some point you…

3 years ago

Back Up Your Raspberry Pi Virtually

Here's a little trick I use to back up my Raspberry Pi without having to…

4 years ago

Unclutter Your GMail Without Missing Out On Your Newsletters

I try to keep my email inbox fairly clean but I do subscribe to some…

4 years ago

Windows Reinstall: Use Secret Migration Tool to Restore All Settings Even If Option Disabled; Plus Tips to Restore Programs After a Clean Install

I was recently faced with the dreaded prospect of re-installing Windows 10. While the standard…

4 years ago

Pi-hole & Unlimited Whitelist for OpenDNS, Cleanbrowsing, Etc.

Pi-Hole and Upstream DNS Providers Pi-hole is an excellent whole-home, self-hosted ad blocker and DNS…

4 years ago