Showing posts with label kill. Show all posts
Showing posts with label kill. Show all posts

Wednesday, October 3, 2018

what is the difference between kill -15 vs kill -9 in linux?


kill -15 vs kill -9
List of kill signals.

kill -l

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX


SIGKILL (9) – Kill signal. Use SIGKILL as a last resort to kill process. This will not save data or cleaning kill the process.

SIGTERM (15) – Termination signal. This is the default and safest way to kill process.

The difference between 9 and 15 is, the default action for both of them is to terminate
the process, but 9 can't be caught or ignored. So if signal 9 gets sent, the process is
guaranteed to die, immediately. If a sig 15 is sent, the process can catch it and perform cleanups.


Signal name
Signal value
Effect
SIGHUP
1
Hangup
SIGINT
2
Interrupt from keyboard
SIGKILL
9
Kill signal
SIGTERM
15
Termination signal
SIGSTOP
17,19,23
Stop the process

Tuesday, November 7, 2017

How to kill defunct or zombie process in linux?

 HOW TO KILL DEFUNCT OR ZOMBIE PROCESS

"defunct" processes is also known as a "zombie" processes. A Zombie process is referred as dead process which is receding on your system though it’s completed executing. In one shot we can say it’s a dead processes which is still in RAM. This process will be in your process table and consuming your memory. Having more defunct process will consume your memory which intern slows your system. We have to kill the defunct process in order to free RAM and make system stable.


What is a zombie process?
When a process finishes execution, it will have an exit status to report to its parent process. Because of this last little bit of information, the process will remain in the operating system’s process table as a zombie process, indicating that it is not to be scheduled for further execution, but that it cannot be completely removed (and its process ID cannot be reused) until it has been determined that the exit status is no longer needed.

When a child exits, the parent process will receive a SIGCHLD signal to indicate that one of its children has finished executing; the parent process will typically call the wait() system call at this point. That call will provide the parent with the child’s exit status, and will cause the child to be reaped, or removed from the process table.


Why defunct process are created?
Ans : When ever a process ends all the memory used by that process are cleared and assigned to new process but due to programming errors/bugs some processes are still left in process table. These are created when there is no proper communication between parent process and child proces

How do I remove zombie processes from a system?
Well, first you can wait. It’s possible that the parent process is intentionally leaving the process in a zombie state to ensure that future children that it may create will not receive the same pid. Or perhaps the parent is occupied, and will reap the child process momentarily.
Secondly, you can send a SIGCHLD signal to the parent (“kill -s SIGCHLD <ppid>“). This will cause well-behaving parents to reap their zombie children.
Finally, you can kill the parent process of the zombie. At that point, all of the parent’s children will be adopted by the init process (pid 1), which periodically runs wait() to reap any zombie children. Then system need reboot to kill zombie process.



Interview questions & answers

1. How to find a defunct process?
Ans : Using ps command
#ps -ef | grep defunct

Or

Run “ps aux” and look for a Z in the STAT column.
Or

ps aux | awk '"[Zz]" ~ $8 { printf("%s, PID = %d\n", $8, $2); }'


2. How can I kill a defunct process?
Ans : Just use kill command
#kill defunct-pid


3. Still not able to kill?
Ans : Then use kill -9 to force kill that process
#kill -9 defunct-pid

4. Still have an issue in killing it?
Ans : Then try to kill it’s parent id and then defunct.

#kill parent-id-of-defunct-pid

Then

#kill -9 parent-id-of-defunct-pid

5. Still having defunct?
Ans : If you still find defunct process eating up RAM then last and final solution is to reboot your machine(This is not preferred on production boxes).

6.What is orphan process?
Ans : An orphan process is said to be a process which runs through parent process is terminated, these process do not know what to do and when to terminate.

7. What is difference between orphan and defunct processes?
Ans : A defunct process is a dead process where there is no execution happening whereas orphan process is a live process which is still in execution state but don't have parent process


Thursday, November 27, 2014

Kill Command Examples in Linux



The Kill command in unix or linux operating system is used to send a signal to the specified process or group. If we dont specify any signal, then the kill command passes the SIGTERM signal. We mostly use the kill command for terminating or killing a process. However we can also use the kill command for running a stopped process.

The syntax of kill command is
kill [-s signal] pid
kill -l

The options to the kill command are:
  • pid : list of process that kill command should send a signal
  • -s signal : send the specified signal to the process
  • -l : list all the available signals.

Let see some of the useful kill command examples in unix or linux system.

Kill Command Examples:

1. Listing all the signal names.

Run the kill command with -l option to list all the available signal names.
> kill -l
HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM USR1 USR2
CLD PWR WINCH URG POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING
LWP FREEZE THAW CANCEL LOST RTMIN RTMIN+1 RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2
RTMAX-1 RTMAX

Some the important signals which we use in our daily usage ar listed below:
Number
Signal Name
Description
0
SIGNULL
Used to check access to the process id
1
SIGHUP
Hup signal. Terminates the process.
2
SIGNINT
Interrupt signal. Terminating the process
3
SIGQUIT
Quit signal. Terminate process with core dump
9
SIGKILL
Forcibly killing a process
24
SIGSTOP
Pausing the process
26
SIGCONT
Runs a stopped process

To know more about a signal, check in man pages. To know about the signal 9, run the below man command:
man 5 signal

2. Getting the process id

To know the process id of a process running in the unix system, use the ps command as
ps -aef
root  4529   657   0   Jul 20 ?           0:06 /usr/local/sbin/sshd -R
....
....

The second field in the output is the process Id. Here the /usr/local/sbin/sshd -R is running with the process id 4529.

3. Killing a process.

To kill processes simply pass the process id to the kill command. This is shown below:
kill 4529

4. Forcefully killing a process.

Use the -9 option with the kill command to kill a process force fully. The following kill command terminates the process forcefully:
kill -9 1567
kill -SIGKILL 1567
kill -KILL 1567
kill -s SIGKILL 1567
kill -s KILL 1567

Avoid using the kill -9 for terminating a process. This will cause memory leaks in the operating system and leads to many other issues.