trap – signal handling in shell

Ways for shell to create ‘multi-thread’ scripts (用shell写多线程脚本)

An enhancement for ‘tod‘(利用trap改进多线程shell脚本)

Yes, the way to write ‘multi-thread’ for shell is using ‘&’ to submit background job. We will use ‘tod’ for example: In ‘tod’, there are in total 3 shell scripts involved – main script ‘tod’, sub script ‘todss’ and sub script(cmd) ‘tcpdump’. Both ‘todss’ and ‘tcpdump’ are called in ‘tod’ as background job. The main purpose of ‘todss’ is to do detection of ‘tod’ to see if it is still alive or killed by user already. For the later case, ‘todss’ would kill the background ‘tcpdump’ and then exits. Now we are going to use ‘trap’ in ‘tod’ to implement signal handling – usually ^C. In this way, there is no need for use to implement ‘todss’ but only main script ‘tod’ and background ‘tcpdump’. Copy the episode of ‘tod’ here:

# Start main thread for ‘tod’
echo “tod: started”
touch $FILENAME
while true
do
        # daveti: Implement trap here to do signal handling
        # daveti: kill background ‘tcpdump’ and kill the shell itself
        trap “kill $TCPDUMP_PID; echo ‘tod: tcpdump killed’; echo ‘tod: exit’; kill $$” INT

        files=`ls $FILENAME* | wc -l`
        if [ “$files”  -gt 1 ]
        then
                # Only keep the 2 latest files on diskless
                for var in $(ls -t $FILENAME* | sed ‘1,2d’)
                do
                        scp $var $REMOTEMACH
                        rm $var
                done
        fi
        sleep 1
done

De facto, there are 3 different ways for ‘trap’: 1. trap “XXXX” signal-list – Catch the signals listed in ‘signal-list’ and do the corresponding actions ‘XXXX”; 2. trap “” signal-list – Ignore the signals in ‘signal-list’; 3. trap signal-list – Recover the default signal handling for signals in ‘signal-list’. Yes again, ‘trap’ could handle all the signals listed by ‘kill -l’ except ‘Signal 11’ – SIGSEGV. Also, to check the definition of certain signals in keyboard, use ‘stty -a’.

bash-2.05b$ 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     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      33) SIGRTMIN    34) SIGRTMIN+1
35) SIGRTMIN+2  36) SIGRTMIN+3  37) SIGRTMIN+4  38) SIGRTMIN+5
39) SIGRTMIN+6  40) SIGRTMIN+7  41) SIGRTMIN+8  42) SIGRTMIN+9
43) SIGRTMIN+10 44) SIGRTMIN+11 45) SIGRTMIN+12 46) SIGRTMIN+13
47) SIGRTMIN+14 48) SIGRTMIN+15 49) SIGRTMAX-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

bash-2.05b$ stty -a
speed 38400 baud; rows 40; columns 154; line = 0;
intr = ^C; quit = ^; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Programming and tagged , , , , , . Bookmark the permalink.

2 Responses to trap – signal handling in shell

  1. Sup there admin, I basically required to firmly leave a quick observation to mention that I adored your story. Thanks!

  2. Ashley says:

    This is a message to the webmaster. I came to your “trap – signal handling in shell | daveti – blog of Dave(Jing) Tian” page via Google but it was difficult to find as you were not on the first page of search results. I see you could have more visitors because there are not many comments on your site yet. I have found a website which offers to dramatically increase your rankings and traffic to your site: http://www.linklegends.com/free-trial. I managed to get close to 1000 visitors/day using their services, you could also get lot more targeted visitors from search engines than you have now. Their free trial and brought significantly more traffic to my site. Hope this helps 🙂 Take care.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s