In our everyday lives, we face this situation a lot of times when we have to exit a process forcefully – windows users would be pretty familiar with this, and the ease with which they are able to do so is very good.

Unix users have a slightly complex approach when it comes to exiting a process less gracefully. Unlike windows, you can’t just open the task manager and exit the process. Also, quite few of us are terminal lovers and so we always love the commands that would allow us using the terminal.

Another problem, or everyday scenario that a lot of us, particularly software testers, is to find something useful or finding an error information from a log. This is like a day to day necessity that a lot of us have, and it is important that you know how to use the terminal properly, because it makes this tedious job a lot easier. I have seen scenarios where a tester is made to go through a log file, which is having -like a thousand lines of log information, probably from months, and is in need to having to find some specific information in it.

So today, we’ll be looking at this two problems and how Unix gives us the command to solve these.

TERMINATING A PROCESS IN UNIX/LINUX/MACINTOSH

Now since Linux and Mac are based on Unix architecture, so a lot of the commands given are same and would do the same work on either Linux and Mac.

To terminate or exit a process in Unix, there is a very good command called the kill command.

Kill command send a signal, a specified signal to be more perfect to a process. The kill command can be executed in a number of ways, directly or from a shell script.

Using kill command from

 /usr/bin

provide you some extra feature to kill a process by process name using

 pkill

. The common syntax for kill command is:

# kill [signal or option] PID

where PID is the process id of the specific process that you want to exit or terminate.

The Signal or Option can take the following values

Signal Name      Signal Value             Behaviour
SIGHUP		  1			  Hangup
SIGKILL		  9			  Kill Signal
SIGTERM		  15		          Terminate

From the behaviour above SIGTERM is the default and safest way to kill a process. SIGHUP is less secure way of killing a process as SIGTERM. SIGKILL is the most unsafe way among the above three, to kill a process which terminates a process without saving.

In order to kill a process, we need to know the Process ID of a process. A Process is an instance of a program. Every-time a program starts, automatically an unique PID is generated for that process. Every Process in Linux, have a pid. The first process that starts when Linux System is booted is – init process, hence it is assigned a value of ‘1‘ in most of the cases.

Init is the master process and can not be killed this way, which insures that the master process don’t gets killed accidentally. Init decides and allows itself to be killed, where kill is merely a request for a shutdown.

To know all the processes and correspondingly their assigned pid, run.

ps -A

You can see an output like this – this is from my Mac terminal

2017-09-22_1417

Before we step ahead and execute a kill command, some important points to be noted:

  1. A user can kill all his process.
  2. A user can not kill another user’s process.
  3. A user can not kill processes System is using.
  4. A root user can kill System-level-process and the process of any user.

To kill a process we already gave the command above. Now let’s suppose, Skype is running on our system, with a given PID.

2017-09-22_1542

As per this, the PID for Skype is 10101, and so to terminate this or kill this process we’d write

kill -9 10101

Now instead of using the signal value, you can also use the Signal name to achieve the same objective.

kill -SIGTERM 10101

or

kill -SIGKILL 10101

KILLING MULTIPLE PROCESSES AT ONCE

To kill multiple processes at once, you just need to provide the PID’s of the process that you would like to be killed.

kill PID1,PID2,PID3
or
kill -9 PID1,PID2,PID3
or
kill -SIGKILL PID1,PID2,PID3

KILLING A PARENT PROCESS

If there is a process that has multiple childs or instances of it created, you can kill all those instances at once using the killall command

Here we don’t use the PID, but instead the process name.

killall [signal or option] processname

For example, if there are many Chrome tabs open and I want to exit all of them at once, then I would do something like

killall -9 chrome