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

Creating a Shortcut to Windows Terminal (Or Any Other App That Doesn’t Show Shortcut Option)

Weirdly, Windows Terminal does not allow you to create a shortcut - there's no obvious…

4 hours ago

Simplest Wireguard Setup Ever

Wireguard Wireguard is the newest way to setup a VPN for your home servers. What…

1 month ago

Fix Your Smart Home – Stop Tasmota Devices From Switching Randomly

While working on fixing my KuLED light switches from magically but unexpectedly switching on, I…

4 months ago

Using WP-CLI to Access MySQL Running in A LAMP Docker Container

WP-CLI is a great command line tool to help you manage Wordpress. I had trouble…

7 months ago

Dunzip – Download and Unzip in Linux With One Command

Tired of downloading zip or tar files, decompressing, and then having to delete the compressed…

7 months ago

Backing Up an SD Card or Hard Drive to A VHD File (Virtual Hard Disk)

  The Microsoft Virtual Hard Disk - An Ideal Backup Image Format Microsoft's Virtual Hard…

7 months ago