Visit The New, Modern Unix Linux Community


Signal Processing


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Signal Processing
# 1  
Question Signal Processing

Hello,
Can any body give example of using Unix Signals. What I want to do is
I am running a sql query in a shell script
I want, if sql query exceed the defined no. of seconds limit, then I would like to kill the process.

I know this can be done thru Unix Signal Handling but I do not know how ?

Thanks
Sanjay
sanjay92
# 2  
Network

I have the following example in one unix book but some how it is not working as supposed to. I do not see the echo message
echo "Got SIGALRM, cmd took too long." which AlarmHandler should display when timeout. ALthough I have noticed that it is killing the process (sleep 60 &) after the timeout.

Can anybody explain me what step, I am doing wrong ?

Thanks
Sanjay



#! /bin/ksh

AlarmHandler() {
echo "Got SIGALRM, cmd took too long."
KillSubProcs
# exit 14
}

KillSubProcs() {
kill ${CHPROCIDS:-$!}
if [ $? -eq 0 ] ; then echo "Sub-processes killed." ; fi
}

SetTimer() {
DEF_TOUT=${1:-10};
if [ $DEF_TOUT -ne 0 ] ; then
sleep $DEF_TOUT && kill -s ALRM $$ &
CHPROCIDS="$CHPROCIDS $!"
TIMERPROC=$!
fi
}

UnsetTimer() {
kill $TIMERPROC
}

# main()

trap AlarmHandler ALRM
SetTimer 4
# Command to run
# For Testing
sleep 60 &
#
CHPROCIDS="$CHPROCIDS $!"
wait $!
echo $?
UnsetTimer
echo "All Done."
#exit 0
sanjay92
# 3  
I have never been able to get ksh to work right with SIGARLM. I suspect that ksh uses it internally somehow. Change all occurences of ARLM to USR1 in your script and I think it will work. But also try this:
Code:
#! /usr/bin/ksh
mypid=$$
(sleep 10 ; kill -TERM -$mypid) &
sleep 60  ## this takes too long
kill -TERM %1 
exit 0

# 4  
Perderabo,
Thank you very much. You are genius.
I do not whether ALRM signal is not working in ksh or the trap command. In my original post, I can see that ALRM is working but only the echo message are not working.
I replaced ALRM with USR1 and it is now displaying message as well.

Any Idea why trap command not working properly in K shell in case of ALRM signal. I have tried INT Signal and it is working fine.


#/bin/ksh

trap "echo hello" ALRM
kill -s ALRM $$
sanjay92
# 5  
Hello Perderabo,

#! /usr/bin/ksh
mypid=$$
(sleep 120 ; kill -TERM -$mypid) & ## This will send signal when
## 120 seconds will pass.
sleep 10 ## this is the command which I want to run.
## which will take 10 seconds.
kill -TERM %1
exit 0

When this is executed, you will still see that one background process against sleep still be running even after completion of the shell job and that process will be sleep and that sleep command will be active for 120 seconds. Is there any way, we can terminate/kill this job after finishing the sleep 10 job.
In short, I would like to kill the sleep process, if the command which I want to run (in our example it is sleep 10) is taking < 120 seconds.

Do you understand what I want to say.

Thank for your help.
Sanjay



Smilie
sanjay92
# 6  
I have never figured out why I can't get ARLM to work with ksh. I just use another signal.

I know that my method leaves a sleep process running if the timed command completes in time. I think that's harmless. It will be reaped by init when it exits. That's the only real cost of a very easily coded solution. Capturing and killing that last pid will drive script back up in complexity.
# 7  
Yes, if Perderabo continues to razzle and dazzle the board with his deep technical contributions; I'm considering changing his forum title to 'Forum Genius'....... great work and thanks for the excellent contributions Perderabo!!!

Also, while we are on the subject, thanks all the forum members, new and old, who take the time to be patient and help others is so many ways, each and every day. All of you make the combined 'Genius' of the forum better and better each day.

BTW, I could go on and on about the Genius in the forums... including praises to PxT, LivinFree, Optimus_P, mib, 98_1LE, rwb1959... for other major contributors and forum-gurus, visit:

https://www.unix.com/memberlist.php?w...ers&perpage=10

Thanks again. -Neo
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #131
Difficulty: Easy
UNIX certification is based on the 'Single Unix Specification' which is an extension of IEEE 1003 (POSIX).
True or False?

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Continue Processing after a signal is caught

Is it possible to continue after signal is caught and control goes to function specified in the trap statement? (3 Replies)
Discussion started by: Soham
3 Replies

2. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

3. Shell Programming and Scripting

How to make parallel processing rather than serial processing ??

Hello everybody, I have a little problem with one of my program. I made a plugin for collectd (a stats collector for my servers) but I have a problem to make it run in parallel. My program gathers stats from logs, so it needs to run in background waiting for any new lines added in the log... (0 Replies)
Discussion started by: Samb95
0 Replies

4. Solaris

Signal Processing in unix

I've read the man page of singal(3) but I still can't quite understand what is the difference between SIGINT, SIGALRM and SIGTERM. Can someone tell me what is the behavioral difference among these 3 signals in kill command? Thanks! (2 Replies)
Discussion started by: joe228
2 Replies

5. Programming

alarm signal processing

I'm writing a function right now, and I want to set an alarm to avoid a timeout, here's the general idea of my code: int amt = -2; alarm(10); amt = read(fd, &t->buf, TASKBUFSIZ - tailpos); //do a read when the alarm goes off, i want to check the value of "amt" ... (1 Reply)
Discussion started by: liaobert
1 Replies

6. Programming

Signal processing

We have written a deamon which have many threads. We are registering for the SIGTERM and trying to close main thread in this signal handling. Actually these are running on Mac OS X ( BSD unix). When we are unloading the deamon with command launchctl, it's sending SIGTERM signal to our process... (1 Reply)
Discussion started by: Akshay4u
1 Replies

7. Shell Programming and Scripting

Script Signal Processing

I am trying to develop a script that will properly handle kill signals particularly kill -2. I have program (_progres) that properly receives the signal if I run it from the command line directly: _progres -T /tmp -p /home/mejones/signal.p -b 2>&1 & If I try to put it in a script (i.e.... (2 Replies)
Discussion started by: mejones99
2 Replies

Featured Tech Videos