Simple shell script to kill an application


Shell scripts are awesome. One can do whatever with a shall script. With  whatever i mean whatever deadly or creative.

However in this article I will try to show how can a simple script be used to kill an application. What ever application you start from a command prompt. Now the question is what is the difference between a Application and a Process.  Well, a process is a simple entity that runs in the memory with its own stack memory and resources. Advanced processes can have multiple threads running simultaneously. But an Application is a combination of processes running together and maintaining inter process communication to handle many tasks one the same time. Typically an application is a large project under the hood of  some group of processes.

Now lets invoke a process for Gnome-Commander with the command

nohup gnome-commander &

This nohup is special command to run a process from terminal and isolate it’s standard I/O from the terminal. The & after the command will run the command in background so you can use the terminal for other tasks.

Now lets see the process in the process list. Hit the command

ps

or

ps -Al

With the -Al you will get all the currently running process in your privilege. But without the -Al argument you will get the process-list invoked from current terminal.

Now time to kill the processes. To kill a process simply invoke kill -9 [process_id] and this [process_id] will you get from the process-list returned by ps.

The kill command basically sends various type of signal to a process. You can see all type of signals with kill -l and will find SIGKILL as numbner 9. So when you invoke kill [process_id] the kill command actually sends a SIGKILL signal to that process of id [process_id]. This is a signal to terminate the process and approximately all the process handles this signal by terminating itself.
kill_gnome-commander

Up to now this is very simple. Now lets write a script out of this knowledge to kill an application. To find out how an application runs lets run ‘Chromium-Browser’ application with the command
nohup chromium-browser &
Now watch how many process supports this application with
ps -Al | grep chromium
You will find a bundle of processes running behind the application.

Now lets write a script that will close an application(ie. Chromium-Browser). Create a file and name it ‘killproc‘ and write #!/bin/bash at the very first position of the file. Note that if the shell finds ‘#!’ at the starting of the file than it will treat this file as a script and will run it in kernel.

So our first step is to take an application name or a name fragment as the command line argument.


#!/bin/bash
#This script will kill a particular process by name expression

if [ $# -lt 1 ]; then
	echo "Process name missing !!"
	exit 1
...

Here the $# indicates the number of command line arguments and $1 or $2 holds the first or second arguments and so on.
The next step is to get the process list with ps command. Now to filter out our desired process we need to pipeline the output of ps to another command awk which is actually nice tool to filter in rows and columns.


#!/bin/bash
#This script will kill a particular process by name expression

if [ $# -lt 1 ]; then
	echo "Process name missing !!"
	exit 1
else	
	# List all the process ID in a line
	list=($(ps | awk '{if((index($4,"'$1'")>0) && ($1!="PID")) {print $1}}'))
	echo "Process count: "${#list[*]}
	max=${#list[*]}

	# If no process found exit
	if [ ${#list[*]} -eq 0 ]
	then
		echo "No process found of this name."		
		exit 0
	fi

	...	

Our filtering command is ps | awk '{if((index($4,"'$1'")>0) && ($1!="PID")) {print $1}}'. With this command we are actually getting a list of process id associated to our application. Then fill an array with this list of processes by list=($(...)) where … is some command that produce a list.

Nest step is to loop through this list of process ids’ and kill them one by one.


#!/bin/bash
#This script will kill a particular process by name expression

if [ $# -lt 1 ]; then
	echo "Process name missing !!"
	exit 1
else	
	# List all the process ID in a line
	list=($(ps | awk '{if((index($4,"'$1'")>0) && ($1!="PID")) {print $1}}'))
	echo "Process count: "${#list[*]}
	max=${#list[*]}

	# If no process found exit
	if [ ${#list[*]} -eq 0 ]
	then
		echo "No process found of this name."		
		exit 0
	fi

	# Loop through all the process and kill one by one
	for ((i=0; i<$max; ++i )); 
	do
		echo "Killing process..."${list[$i]}
		kill -9 ${list[$i]}    		
	done

	#Final Message
	if [ $? -eq 0 ]; then
		echo "All process killed sucessfully."
	else
		echo "Proess killed ..."
	fi

fi

With a for loop we traverse through the process ids and kill them one by one with kill -9 ${list[$i]}. Finally we give a friendly message.

Now to kill a ‘Chromium-Browser’ application we simply invoke


     killproc chromium

This will kill all the associated processes with Chromium-Browser.

killproc

Try it yourself and have fun.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s