LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2012, 08:10 AM   #1
crimsonman
LQ Newbie
 
Registered: Mar 2012
Posts: 9

Rep: Reputation: Disabled
question about trap and signal


i don't understand about trap and signals. why do i have to use it and how can i use it?
 
Old 04-06-2012, 09:03 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Unix signal is a kind of event sent asynchronously to your program by other programs. trap allows you to declare signal handler. For example your script may create some temporary files which should be deleted when script exits. But if user press Ctrl+C, thus sending SIGINT signal to your program, the program exits immediately, leaving a mess of temporary files on the disk. To handle SIGINT signal:
Code:
trap 'rm $TEMPFILE' INT
Some signals, like USR1, USR2, are often used to reload configuration files without stopping the program.
Signals may be sent using the kill utility.

See man 1 kill, help trap, man -k kill for details.

Hope that helps.
 
Old 04-06-2012, 03:22 PM   #3
crimsonman
LQ Newbie
 
Registered: Mar 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
thx a lot, i'll try to get into it.
 
Old 04-07-2012, 12:39 AM   #4
crimsonman
LQ Newbie
 
Registered: Mar 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
i use trap to react when i send a signal INT to kill a process. the process can detect a signal, but it isn't stop.

i use sleep command in the process that i want to kill. anyone knows what's going on?

Main Script
===================================
#!/bin/bash
echo $$

function initKill() {
echo "Receive SIGINT"
kill -9 $$
exit 1
}

trap initKill SIGINT

sleep 60
===================================

Kill Script
===================================
#!/bin/bash

kill -SIGINT $1
===================================
 
Old 04-07-2012, 01:41 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Here is a quote from man bash:
Quote:
If bash is waiting for a command to complete and receives a signal for which a trap has been set, the
trap will not be executed until the command completes. When bash is waiting for an asynchronous com‐
mand via the wait builtin, the reception of a signal for which a trap has been set will cause the wait
builtin to return immediately with an exit status greater than 128, immediately after which the trap
Because SIGINT by default causes process to exit, you may send it to whole process group. It will cause pause 60 to exit and then the signal handler of your script will be executed. To send a signal to process group, negate the pid (add minus sign) in the kill command line.
 
Old 04-07-2012, 11:41 AM   #6
crimsonman
LQ Newbie
 
Registered: Mar 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
thank you a lot ^^
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Program received signal SIGTRAP, Trace/breakpoint trap while testing the arm elf chanducnu Programming 0 01-03-2012 05:19 AM
how to send snmp trap & recieve trap in C program minil Programming 3 07-10-2010 09:22 AM
signal handling question fox_hound_33 Programming 4 05-28-2008 03:19 AM
Kernel trap (Fatal trap 12) m!k@EL *BSD 4 09-05-2007 11:58 PM
signal() question. JanusPaul Programming 6 12-03-2004 09:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration