script_1:
Use trap command to setup the customized handler for signal HUP and TERM
#! /bin/bash
trap "echo Catch the HUP signal!" HUP
trap "echo Catch the terminal signal! ; rm ./temp ; exit 0" TERM
echo "" >temp
a=1
while [ $a -ne 0 ]
do
((a++))
echo $a >>temp
sleep 5
done
terminal:
1) Launch the script_1 process
2) Run ps command and script_1 process is already launched
3) Output the temp file which is generated by script_1, the process is adding text into the file
4) Send the HUP signal to script_1 process
5) script_1 process catch the HUP signal and output the text on standard output
6) Run ps command, but the script_1 process is still alive
7) List the temp file, temp file is not deleted
8) Send the TERM signal to script_1 process
9) script_1 process catch the TERM signal and output the text
10) Run the ps command, script_1 process is already removed
11) List the temp file, temp file is already deleted.
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 &
[2] 3448
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps
PID TTY TIME CMD
2954 pts/0 00:00:00 bash
3162 pts/0 00:00:09 emacs
3448 pts/0 00:00:00 script_1
3449 pts/0 00:00:00 sleep
3450 pts/0 00:00:00 ps
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat temp
2
3
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ kill -HUP 3448
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ Catch the HUP signal!
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps
PID TTY TIME CMD
2954 pts/0 00:00:00 bash
3162 pts/0 00:00:09 emacs
3448 pts/0 00:00:00 script_1
3456 pts/0 00:00:00 sleep
3457 pts/0 00:00:00 ps
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt temp
-rw-rw-r-- 1 aubinxia aubinxia 15 Jul 26 17:17 temp
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ kill -TERM 3448
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ Catch the terminal signal!
[2]+ Done ./script_1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps
PID TTY TIME CMD
2954 pts/0 00:00:00 bash
3162 pts/0 00:00:09 emacs
3462 pts/0 00:00:00 ps
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt temp
ls: cannot access temp: No such file or directory
2. Other Signals -EXIT
EXIT signal is caught before the exit() system call is called.
./script_1:
#! /bin/bash
trap "echo Catch the EXIT signal" EXIT
a=1
while [ $a -ne 0 ]
do
date >/dev/null
sleep 5
done
terminal:
1) Launch the script_1 process
2) kill the process script_1 and it catch the EXIT signal
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 &
[2] 3524
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ kill -HUP 3524
Catch the EXIT signal
3. Other Signals -DEBUG
script_1:
#! /bin/bash
trap "echo Catch the DEBUG signal" DEBUG
a=2
echo "Hello world! a=" $a
#output:
#Catch the DEBUG signal:
#Catch the DEBUG signal:
#Hello world! a= 2
#
#This indicates that DEBUG signal handler is launched
#before each statement
((a++))
echo "Hello world! a="$a
#output:
#Catch the DEBUG signal
#Catch the DEBUG signal
#Hello world! a=3
#
#This also indicates that DEBUG signal handler is launched
#before each statement.
4. Other signal -ERR
script_1:
#! /bin/bash
trap "echo Catch the ERR signal" ERR
echo "file information: " $(ls no-such-file)
#output:
#ls: cannot access no-such-file: No such file or directory
#file information:
ls no_such_file
#output:
#ls: cannot access no_such_file: No such file or directory
#Catch the ERR signal
It indicates that only the 2nd "ls" command catch the ERR signal.
No comments:
Post a Comment