ksh interrupt read instruction with signal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh interrupt read instruction with signal
# 8  
Old 11-30-2011
Try putting exit in your trap command:

Code:
trap 'echo "trapped USR1" ; exit ' USR1

# 9  
Old 12-02-2011
Chubler_XL, thanks, return works well but this is not desired action for my case.

Corona688, ygemici - thank you for explanation, now I know why.
I tried to use this knowledge and modify my script a bit and workaround it.
Please see better version below.
I created dedicated _read function, doing nothing more than calling read.
I closed it in the while loop to make sure it works more than once.
And, I'm very close...

Code:
#!/bin/ksh

typeset TEXT=""
typeset TMOUT=0

function _read
{
        read $@
}

function get_status
{
        while true
        do
                (( i += 1 ))
                #sleep 2
                #echo "$i sleep a while on the start"

                trap '{
                        print "Got a signal!"
                        continue
                }' USR1

                echo "$i Running background job and asking for the input"
                ppid=$$
                {
                        sleep $TMOUT
                        echo "$i time is up..."
                        echo "$i sending sig to ${ppid}"
                        kill -s USR1 ${ppid}
                } &
                bgpid=${!}
                _read TEXT
                kill -9 ${bgpid}
                echo "$i doing something: $TEXT"
        done
}

get_status

And, it works pretty nice. Almost Smilie
It goes in some race condition in don't understand yet.
Please see output.


Code:
[Linux] # ./a2.ksh
1 Running background job and asking for the input
1 time is up...
1 sending sig to 9277

Got a signal!
2 Running background job and asking for the input
2 time is up...
2 sending sig to 9277

Got a signal!
3 Running background job and asking for the input
3 time is up...
3 sending sig to 9277

....
# and it's running.....
# but finally:
...

1596 Running background job and asking for the input
1596 time is up...
1596 sending sig to 9277
Got a signal!
1597 Running background job and asking for the input
1597 time is up...
1597 sending sig to 9277

Got a signal!
User signal 1

It died killed by USR1. I'm surprised, since I have dedicated trap for it which is defined at this time.

--bzk

---------- Post updated at 08:15 AM ---------- Previous update was at 06:55 AM ----------

New version of script.
Much more stable now.
Changes:
send KILL signal to the background process from the "trap".
make sure signal was not only sent, but also background process is not running any more.
I will let it work for couple of hours and let you know.

Code:
#!/bin/ksh

typeset TEXT=""
typeset TMOUT=0

function _read
{
        read $@
}

function get_status
{
        while true
        do
                (( i += 1 ))
                #sleep 2
                #echo "$i sleep a while on the start"

                trap '{
                        print "Got a signal!"
                        # Clean up bg process
                        kill -9 ${bgpid}

                        # make sure bg process is not running
                        kill -0 ${bgpid} > /dev/null 2>&1 &&
                        {
                                echo "Still running ${bgpid}"
                                sleep 1
                        }
                        continue
                }' USR1

                echo "$i Running background job and asking for the input"
                ppid=$$
                {
                        sleep 0
                        echo "$i time is up... sending sig to ${ppid}"
                        kill -s USR1 ${ppid}
                        echo "$i kill rc=$?"
                } &
                echo "$i sent in background, rc: $?"
                bgpid=${!}
                _read TEXT
                kill -9 ${bgpid}
                echo "$i doing something: $TEXT"
        done
}

get_status

thanks
--bzk

---------- Post updated 12-02-11 at 04:40 AM ---------- Previous update was 12-01-11 at 08:15 AM ----------

OK, it works. but remove TMOUT from the last script I provided.
TMOUT variable has different meaning in ksh88 and ksh93. See man below:

Ksh93:
TMOUT If set to a value greater than zero, TMOUT will be the default
timeout value for the read built-in command. The select comĀ*
pound command terminates after TMOUT seconds when input is from
a terminal. Otherwise, the shell will terminate if a line is
not entered within the prescribed number of seconds while readĀ*
ing from a terminal. (Note that the shell can be compiled with
a maximum bound for this value which cannot be exceeded.)

ksh88:
TMOUT If set to a value greater than zero, the
shell terminates if a command is not entered
within the prescribed number of seconds after
issuing the PS1 prompt. The shell can be com-
piled with a maximum bound for this value
which cannot be exceeded.

Once again thanks for showing me the right track!
bzk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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! :D 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... (2 Replies)
Discussion started by: jakesalomon
2 Replies

2. Shell Programming and Scripting

ksh while read issue

Hello, I have used a chunk of ksh script similar to this in many places without any issue: while : do print; read OPTION?"Enter a number (q to quit): " expr ${OPTION} + 1 >/dev/null 2>&1 CHECKVAL=$? if }" != ${OPTION} ]; then ... (2 Replies)
Discussion started by: port43
2 Replies

3. UNIX for Advanced & Expert Users

Is there any shell command to show which interrupt handler handle which interrupt number?

Hi, all: Is there any shell command to show which interrupt handler handle which interrupt number in the system? li,kunlun (5 Replies)
Discussion started by: liklstar
5 Replies

4. UNIX for Advanced & Expert Users

Interrupt storm detected on "irq 20" throttling interrupt source

I receive the following warning messages on a very new machine which has FreeBSD 8.1 x64 installed on it: Interrupt storm detected on "irq 20" throttling interrupt source It is unclear what this means and what its origins are (motherboard? CPU? RAM?). I can start the desktop and the message is... (4 Replies)
Discussion started by: figaro
4 Replies

5. UNIX for Dummies Questions & Answers

simple way to read an array in ksh

hi, I'm a newbie to shell scripting. I wanted to initialise an array using basic for loop and read it. Then i want to print it as a .CSV file.. Any help would me much appreciated.. (1 Reply)
Discussion started by: pravsripad
1 Replies

6. Programming

how to discard instruction from previous signal

hello everyone, I'm having a problem doing signal handling so I post this thread to see if I could get help. I want asynchronous signal handling, that means when I'm processing a signal (signal 1), if the same signal comes (signal 2) that signal (signal 2) shall be processed; and moreover,... (7 Replies)
Discussion started by: nvhoang
7 Replies

7. Shell Programming and Scripting

read from console in ksh

I am stuck with a problem while reading data from a file.. while do read line #do some operations and if some condition is satisfied, ask the user to enter his choice. # using the choice continue operations. done < fileBeingRead.txt The... (4 Replies)
Discussion started by: ajaykumarns
4 Replies

8. Shell Programming and Scripting

is there any way to read a line twice in KSH

Hi All, Is there any way to read the previous line in file reading ? or is there any way to read a line twice in KSH ? thanks in advance !! Srini (6 Replies)
Discussion started by: Srini75
6 Replies

9. Shell Programming and Scripting

ksh read timeout

any idea on how to timeout the read statement for ksh? for bash u can use read -t option -t timeout Cause read to time out and return failure if a complete line of input is not read within timeout seconds. This option has ... (2 Replies)
Discussion started by: ashterix
2 Replies

10. UNIX for Advanced & Expert Users

Interrupt signal Control C takes too long to terminate a process

I have a process to terminate, and when keying Control C/ kill -int , it takes 15 minutes to half an hour to terminate the process. I've tried using kill -2, or keying control c twice, however the process seem to be killed abruptly, without writing into the log file. So the only way in order to... (8 Replies)
Discussion started by: paqui
8 Replies
Login or Register to Ask a Question