Linux #26 : Process 종료 및 그에 따른 간단한 설명

Linux System을 운영하다보면 특정 Process를 죽이기 위해 “kill” command를 사용하는 경우가 있죠. “/etc/init.d/” 안에 Control Lib Script로 등록되는 일반적인 Process는 비정상적인 경우가 아니면 “kill”을 사용할 필요가 없는데 경우에 따라서 “kill”을 통해서 Process를 죽여할 때가 있습니다.

시작은 어떤 Process를 죽여야하고 그 Process의 pid(Process ID)가 무엇인지를 확인하는게 필요하죠.

# ps ax
PID TTY STAT TIME COMMAND
1 ? Ss 2:51 init [3]
2 ? S 0:00 [kthreadd]
3 ? S 0:43 [migration/0]
4 ? S 0:16 [ksoftirqd/0]
5 ? S 0:36 [migration/1]
19331 ? Sl 3:57 /sbin/rsyslogd -c5
22222 ? S 1111:11 /usr/local/sbin/proftpd
22560 tty1 Ss+ 0:00 /sbin/mingetty --long-hostname tty1
23384 ? S 0:00 /usr/local/apache2/bin/httpd -k restart
24406 ? S 0:05 /usr/local/apache2/bin/httpd -k restart
24605 ? Ss 2:20 /usr/local/apache2/bin/httpd -k restart
25081 ? S 0:00 /usr/local/apache2/bin/httpd -k restart

“ps ax” command를 통해서 확인하면 대충 이렇겠죠. “ps -elf”라고 입력하면 더 많은 정보가 나옵니다. “ps”에 대해서는 다음시간에 더 자세히 다루도록 하구요.

예로 proftpd daemon을 죽일예정이고,

# ps ax | grep proftpd
22222 ? S 1111:11 /usr/local/sbin/proftpd
..

이런 식으로 표시가 되겠죠. 그리고, pid가 22222 인걸 알 수 있죠. 기본적으로 “kill [pid]”와 같이 실행을 하게 되는데 default로 -TERM이 지정되게 됩니다. 즉

# kill -TERM 22222 = kill 22222

와 같은 것이죠. 여기서 TERM은 Terminate 입니다. “kill” command에서 지정 가능한 Signal은 여러 종류가 있습니다. “-l” option을 지정하면 볼 수 있는데,

# 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

이렇게나 많아요. 실제 “kill”에서 사용할 때는 앞의 SIG를 제외하고 예를 들면 1) SIGHUP는 HUP(Hang UP), 2) SIGINT는 INT(Initial) 으로 처리하게 됩니다.
많이 쓰는 건 앞에서 말씀드린 15) SIGTERM 이네요. Default이기도 하고

따라서, 좀 전에 22222 PID를 “kill” 하면 아래와 같이,

#kill -TERM 22222 or kill 22222
[1]+ TErminated /usr/local/sbin/proftpd

와 비슷하게 처리가 될 겁니다. 그런데, “kill” command로 죽일 수 없는 Processs들을 언젠가 만나실 수 있는데 Zombie Process라고도 합니다. 이런 Process들을 죽이려면 때로는 System을 Reboot 해야하는 경우도 있죠. Process의 Status는 위에 “ps ax”의 option 중 “a”가 추가 되었을 때 확인 가능합니다.

# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 10356 476 ? Ss Apr07 2:51 init [3]
root 2 0.0 0.0 0 0 ? S Apr07 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Apr07 0:43 [migration/0]
root 4 0.0 0.0 0 0 ? S Apr07 0:16 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S Apr07 0:36 [migration/1]
root 6 0.0 0.0 0 0 ? S Apr07 0:17 [ksoftirqd/1]
..
www 23384 0.0 0.0 219396 8316 ? S 06:08 0:00 /usr/local/apache2/bin/httpd -k restart
www 24406 0.0 0.1 225404 18756 ? S May27 0:05 /usr/local/apache2/bin/httpd -k restart
root 24605 0.0 0.0 219264 11172 ? Ss Apr11 2:20 /usr/local/apache2/bin/httpd -k restart
www 25081 0.0 0.0 219528 9588 ? S 05:42 0:00 /usr/local/apache2/bin/httpd -k restart

좀 더 자세하게 볼 수 있도록 “u” option을 더 추가했습니다.

Process의 Status Code를 설명 해 드리면,

PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a
process.
D Uninterruptible sleep (usually IO)
R Running or runnable (on run queue)
S Interruptible sleep (waiting for an event to complete)
T Stopped, either by a job control signal or because it is being traced.
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z Defunct ("zombie") process, terminated but not reaped by its parent.

For BSD formats and when the stat keyword is used, additional characters may be displayed:
< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group

아참 꼭 “man” command를 통해 Linux 일반적인 command들의 내용을 참조하는 습관을 기르세요. 최고의 Guide라고 생각이 들어요. 아래 따로 설명을 드렸지만 위 Man page의 내용을 보면 사실 필요없지 않나 생각이 드네요.

D : IO 순번을 기다리는 상태
R : 실행 중
S : 대기 중
T : 중단 중
W : Swapout 된 상태
X : 죽은 상태
Z : Zombie

< : 높은 우선 순위로 실행 중
N : 낮은 우선 순위로 실행 중
L : Real Time 처리 등으로 메모리내에 고정 되어 실행 중 등등

여기서 Zombie는 CPU Time은 소비하지 않는데, Physical Memory나 Swap Memory를 사용하고 있는 경우가 있습니다. 따라서, 계속 늘어나게 된다면 Memory Leak이 발생을 하겠지요.
간단하지만, 알아두면 좋을 듯 하네요.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.