Sponsored Content
Top Forums Shell Programming and Scripting Signal trapped during read resumes sleeping Post 302977235 by jakesalomon on Thursday 14th of July 2016 07:31:34 PM
Old 07-14-2016
Blade Signal trapped during read resumes sleeping

Greetings. This is my first post in this forum; I hope y'all find it useful. One caveat: "Concise" is my middle name. NOT! Smilie

I am almost done with a shell script that runs as a daemon. It monitors a message log that is frequently written to by a database server but it it works my client will have more such daemons.

After a few rough starts and things that just don't work, I am using dtksh (M-12/28/93d; the alternative on my client's old Solaris box was ksh 88). Here's how the loop works:
Code:
tail -0f $ONLINELOG |
while true                           # Loop until a break command
do
  if [[ $go_flag -eq $FALSE ]]
  then
    break
  fi
  read newline
<.... Do my stuff, parsing $newline for a significant event ...>
done

Now how do I send this daemon a message to clean up after itself and exit quietly? Well, before I started that loop, I set up a simple, one-signal trap command:
Code:
trap 'echo Interrupt received; go_flag=$FALSE; break' INT

(I'm not even certain that break will do what I think.) All so very logical! So what's the problem? Smilie

Well, it is overwhelmingly likely that when I send it the "kill -2", my daemon will be sleeping on that "read newline" command. My output log shows that it did catch the signal but it goes right back to sleep on that read!

A Google search [ksh signal break read] showed me I am not alone; the first hit was a 5-year-old posting on this very forum. I was quite unhappy with the conclusion of that thread but even if ksh83v (the latest I've heard of) has fixed the issues discussed in that thread, I have no access to that. But I love to tinker so I added this to the trap handler:
Code:
trap 'echo Interrupt received; go_flag=$FALSE; echo "" >>$ONLINELOG; break' INT

What did I do up there?

I fed an extra empty line to the end of the file being tailed. (Remember, I'm not reading directly from the file but from a pipeline.) Hence, that read is immediately satisfied and the process awakens to see an empty line, continues back at the top of the loop, where it sees that $go_flag is $FALSE and breaks out of the loop.

This kluge worked for me, although it's a fair bet it won't work for everyone. And, of course, for all I know the SA_RESETHAND issue discussed in that post has been fixed.

-- JS, who should have been a teacher. Smilie
 

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to wakeup sleeping processes

Hi, Could someone please tell me how to wakeup sleeping processes? (i.e. change the process status from "S" to "R" when viewing in ps command). I ran a few programs in the background by "&" which went into "sleep" mode and I want them to run. Any help will be greatly appreciated. Steve (11 Replies)
Discussion started by: stevefox
11 Replies

2. UNIX for Advanced & Expert Users

autosys sample resumes

Hi all, Can anybody send the autosys developer sample resumes. If this is not the correct place to ask please let me know where can I get them. Your help would be appreciated. Thanks Renuka (0 Replies)
Discussion started by: renukasaga01
0 Replies

3. UNIX for Advanced & Expert Users

Trapped Signal HUP

We encountered an issue in our project while using the Interix UNIX (SFU 3.5) and explained our query below. We would be happy if anybody helps us to troubleshoot the problem J In our code the trapping signal for all signals like HUP, INT, QUIT, ILL, TRAP, ABRT, EXCEPT, etc., is initialized in... (4 Replies)
Discussion started by: RAMESHPRABUDASS
4 Replies

4. Shell Programming and Scripting

Trapped in pipe

Hi Is there any way to find out in a single step ( command) the step where the pipe command failed when using multiple commands using pipe . eg : ll *.tar | grep dec | grep december.tar the first step is listing all tar files . Second step constitutes piping that data and doing grep... (5 Replies)
Discussion started by: ultimatix
5 Replies

5. Shell Programming and Scripting

Help on sleeping the script

Hi all, How can i specify to sleep for 24 hours in a script Thanks Firestar (5 Replies)
Discussion started by: firestar
5 Replies

6. Shell Programming and Scripting

Sleep process not sleeping!

I had a script executing every hour to kill a process. I used loop rather than cron to execute it periodically. But now when I am trying to kill that sleep process of 1 hour its not getting killed. it is taking a new PID everytime I kill. To disable the script commenting is the only option... (1 Reply)
Discussion started by: nixhead
1 Replies

7. Shell Programming and Scripting

ksh interrupt read instruction with signal

Dear shell experts, I spent last few days porting ksh script from ksh88/SunOS to ksh93/Linux. Basically, things are going well and I do not have too much troubles porting ks88 script to ksh93, but I stuck on one item. It's about sending and handling the signal. I found two similar... (8 Replies)
Discussion started by: bzk
8 Replies
trap(1) 							   User Commands							   trap(1)

NAME
trap, onintr - shell built-in functions to respond to (hardware) signals SYNOPSIS
sh trap [ argument n [n2...]] csh onintr [-| label] ksh *trap [ arg sig [ sig2...]] DESCRIPTION
sh The trap command argument is to be read and executed when the shell receives numeric or symbolic signal(s) (n). (Note: argument is scanned once when the trap is set and once when the trap is taken.) Trap commands are executed in order of signal number or corresponding symbolic names. Any attempt to set a trap on a signal that was ignored on entry to the current shell is ineffective. An attempt to trap on signal 11 (memory fault) produces an error. If argument is absent all trap(s) n are reset to their original values. If argument is the null string this signal is ignored by the shell and by the commands it invokes. If n is 0 the command argument is executed on exit from the shell. The trap command with no arguments prints a list of commands associated with each signal number. csh onintr controls the action of the shell on interrupts. With no arguments, onintr restores the default action of the shell on interrupts. (The shell terminates shell scripts and returns to the terminal command input level). With the - argument, the shell ignores all inter- rupts. With a label argument, the shell executes a goto label when an interrupt is received or a child process terminates because it was interrupted. ksh trap uses arg as a command to be read and executed when the shell receives signal(s) sig. (Note that arg is scanned once when the trap is set and once when the trap is taken.) Each sig can be given as a number or as the name of the signal. trap commands are executed in order of signal number. Any attempt to set a trap on a signal that was ignored on entry to the current shell is ineffective. If arg is omitted or is -, then the trap(s) for each sig are reset to their original values. If arg is the null (the empty string, e.g., "" ) string then this signal is ignored by the shell and by the commands it invokes. If sig is ERR then arg will be executed whenever a command has a non- zero exit status. If sig is DEBUG then arg will be executed after each command. If sig is 0 or EXIT for a trap set outside any function then the command arg is executed on exit from the shell. The trap command with no arguments prints a list of commands associated with each signal number. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), exit(1), ksh(1), sh(1), attributes(5) SunOS 5.10 23 Oct 1994 trap(1)
All times are GMT -4. The time now is 09:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy