728x90
반응형
시그널(Signal) 이란?
Signal 이란 프로세나 시스템에 이벤트를 전달하는 신호를 나타냅니다.
일반적으로 흔시 하용하는 Ctrl +C 같은 경우도 Signal의 한 종류이다.
시그널(Signal) 종류
리눅스 콘솔 창 에서 kill -l 명령을 사용하면 시그널 리스트를 확인 할 수 있다.
$ 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
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
$ help kill
kill: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
Send a signal to a job.
Send the processes identified by PID or JOBSPEC the signal named by
SIGSPEC or SIGNUM. If neither SIGSPEC nor SIGNUM is present, then
SIGTERM is assumed.
Options:
-s sig SIG is a signal name
-n sig SIG is a signal number
-l list the signal names; if arguments follow `-l' they are
assumed to be signal numbers for which names should be listed
Kill is a shell builtin for two reasons: it allows job IDs to be used
instead of process IDs, and allows processes to be killed if the limit
on processes that you can create is reached.
Exit Status:
Returns success unless an invalid option is given or an error occurs.
kill: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
Send a signal to a job.
Send the processes identified by PID or JOBSPEC the signal named by
SIGSPEC or SIGNUM. If neither SIGSPEC nor SIGNUM is present, then
SIGTERM is assumed.
Options:
-s sig SIG is a signal name
-n sig SIG is a signal number
-l list the signal names; if arguments follow `-l' they are
assumed to be signal numbers for which names should be listed
Kill is a shell builtin for two reasons: it allows job IDs to be used
instead of process IDs, and allows processes to be killed if the limit
on processes that you can create is reached.
Exit Status:
Returns success unless an invalid option is given or an error occurs.
시그널(Signal) 종류 및 설명
trap을 이용한 Signal 처리 예제
무한으로 수행되는 쉘 코드로 trap 을 이용한 시그널 처리 동작에 대해 알아보겠습니다.
소개하는 코드는 1 초마다 현재 수행되고 있는 loop.sh 프로세스를 확인하고
CTRL +C 시그널을 포착해서 프로그램을 종료하는 예제입니다.
loop.sh 수행 순서
while loop 수행 : loop count : [1]
사용자 CTRL+C (SIGINT:2 시그널 발생) => _int() 함수 호출 => kill -15 (SIGTERM ) 시그널 발생
=>_term() 함수 호출 => kill -9로 loop.sh 프로세스 종료
loop.sh
#!/bin/bash
loop_cnt=0
echo "=================================="
echo "======= LOOP SHELL PROCESS ======="
echo "=================================="
ps -ef|grep loop.sh
process=`ps -ef|grep loop.sh |awk '{print $2}'`
echo "PROCESS_ID:[ $process ]"
########### SIGTERM handler ############
function _term() {
echo "SIGTERM Event call"
kill -9 $process
}
########### SIGINT handler ############
function _int() {
echo "SIGINT Event call"
kill -15 $process
}
trap _term SIGTERM
trap _int SIGINT
while true
do
loop_cnt=`expr $loop_cnt + 1`
echo "loop count : [$loop_cnt]"
sleep 1
done
수행 결과
728x90
반응형
'06.OS > Linux' 카테고리의 다른 글
[CentOS]Host Name 변경 (0) | 2022.03.26 |
---|---|
[Ubuntu] Vmware Ubuntu20.04 LTS 설치 (0) | 2022.03.26 |
Ubuntu SSH 접속 방법 (0) | 2022.03.20 |
Ubuntu 명령어 (0) | 2022.03.19 |
우분투 Chrom(크롬)설치 (0) | 2022.03.09 |