BASH - One liner to kill a process.

Submitted by john on Mon, 04/10/2017 - 10:16

binbashThere are a ton of ways to do this. This is one of many ways to accomplish this task. 

One liners are a pain in the butt to remember exactly how to do, so aliases in your .bash_profile are a win. I will show you how to set that up to make this really nice. 

I have a program called moose_antlers.py and it needs a fresh restart now and then. I only want to type in the word moose in the command line and have it refresh. Here is how I do it:

 

 

 

nano ~/.bash_profile

 alias moose="kill -9 $(ps aux | grep moose_antlers.py | grep -v grep | awk '{print $2}') && /home/user/moose_antlers.py &"

Save your .bash_profile and restart your terminal. Now when you type in moose, your script restarts. Let's break the one liner down.

kill -9 program_processID --- This kill the running program by process ID. 

ps -aux --- This gets a list of the running tasks on your computer. We are going to find the task we want, get the PID, and use that PID in the kill -9 statement. 

$() --- The final output of what is inside of this will be a string to use by something else. A good way to set a command like date to a variable is like this $(date). The old way of doing this is `data`. If you see this out in the wild, replace it. This is deprecated code. 

grep moose_antlers.py --- This picks the line of output that has the name I specified in it. If you don't know grep, this is one of the most important Linux tools. Learn it, learn it, learn it. 

grep -v grep --- Grep again, except -v mean NOT this keyword. You just ran grep before this and that process will show up in the output. Get rid of that. You don't need it. 

awk '{print $2}' -- Awk is a language all to itself with books written just for it. By default awk uses " " (space) as a delimiter. You can change that with -F',' <-- In this example I changed the delimiter to comma. '{}' is a command clause and it will do the things inside the brackets. This is space delimited like most output in CLI and the second item is what I want to slice out. So I chose to print $2. 

| -- PIPE.. Okay, this is simple.. Programs have 3 PIPED items, STDIN, STDERR, and STDOUT. You can pipe date INTO most programs and it will either error out or do something with it and give you output (that can be piped into other programs. 

Std_in Std_out Std_Err 

This is a crude illustration of how a program takes in input, and outputs either out or error. 

&& -- This means do the thing after the && once the command before it completes. Think of it as "and then do this".

Notice that I call my script with a /full/path/to/the/script.. This is a good practice in scripting because sometimes relative paths can get messy. Use absolute paths in scripts whenever possible. 

Last we see that & at the end. That tell my script to run in the back ground. The STDOUT you should get is a process ID of the program running in the background. 

 

I hope this helps someone out there. We will do some more Linux magic in future articles. Stay tuned.