Tuesday, November 22, 2011

UNIX: Terminating a process

You can terminate a process using the KILL command. To KILL a process you need to have the process ID passed to the KILL command.

There are different types of signals that you pass to a process.
1    SIGHUP - Hang Up
2    SIGINT - Interrrupt
9    SIGKILL - Kill Immediate
15  SIGTERM - Terminate

SIGHUP signal is sent to every process you are running just before you log out of the system.
SIGINT signal is sent when you press ctrl+c
SIGKILL - Kills a process immediately. Temporary files are not deleted here.
SIGTERM - Immediate termination of progamme but allows cleaning up the temporary files.

The default signal with KILL command is SIGTERM.

You can specify the signals with KILL command either by their number or by their name without the SIG
eg:-
kill -9 process.sh
or
kill KILL process.sh

Basically there are 30 different signals in UNIX. The above explained are the most important ones.

Example:
Finding the jobs
bash-3.00$ jobs
[2]-  Stopped                 vi
[3]+  Stopped                 vi
Killing the job with % (here we terminates the job)
bash-3.00$ kill %2
bash-3.00$ jobs
[2]-  Done                    vi
[3]+  Stopped                 vi
Finding Jobs:
bash-3.00$ jobs
[3]+  Stopped                 vi
Killing jobs with KILL signal (-KILL is missing)
bash-3.00$ kill KILL %3
bash: kill: KILL: arguments must be process or job IDs
starting a process in the background
bash-3.00$ vi &
[1] 28236
bash-3.00$ jobs
[1]+  Stopped                 vi
Kill process with signal number (kill immediate)
bash-3.00$ kill -9 28236
bash-3.00$ jobs
[1]+  Killed                  vi
Checking for jobs
bash-3.00$ jobs
No jobs left

create new job in bg
bash-3.00$ vi &
[1] 28321

bash-3.00$ jobs
[1]+  Stopped                 vi

Kill with signal number option for termination
bash-3.00$ kill -15 28321

bash-3.00$ jobs
[1]+  Stopped                 vi
bash-3.00$ kill 28321
bash-3.00$ jobs
[1]+  Stopped                 vi
bash-3.00$ kill -KILL 28321
bash-3.00$ jobs
[1]+  Killed                  vi
Note: Inorder to run the kill command for a second time to make sure that the kill works, enter "!!"

No comments:

Post a Comment