Sleep in ksh script ignored until the sleep time expires

Sleep in ksh script ignored until the sleep time expires

Post by Okki Trangga » Sun, 27 Aug 1995 04:00:00



Hi,

I'm trying to write a ksh script that traps certain signals (HUP,
INT, QUIT, TERM).  For testing, I wrote the following script:

   #!/bin/ksh
   trap 'print Signal caught' HUP INT QUIT TERM
   while :; do
      print "Sleeping ..."
      sleep 30
   done

I ran the script and opened another terminal session and issued a 'kill'
command followed by the PID # of the above script (say 11980):
   $ kill -INT 11980

The script above didn't seem to trap the sent signal immediately.  But
instead, completes the sleep (30 seconds) and then acknowledge the signal
(by invoking the command specified after the "trap" built-in command).

I posted a question to IBM, but the AIX Support person was unable to
duplicate the situation.  He indicated during his testing that the signal
was caught by the "trap" command immediately without waiting for the sleep
to expire when he sent a signal using 'kill' to the PID of the ksh script.

The script, however, did get interrupted immediately if I type Ctrl-C
on the same terminal session running the script -- but this won't help me.

Has anyone seen this problem?  Any known solution?

Thanks ..

----------------------------------------------------------------------
Okki Tranggana                    |    

tel:   (408) 974-7323             |    1 Infinite Loop, MS:76-2TI
fax:   (408) 974-1560             |    Cupertino, CA 95014
--
----------------------------------------------------------------------
Okki Tranggana                    |    

tel:   (408) 974-7323             |    1 Infinite Loop, MS:76-2TI
fax:   (408) 974-1560             |    Cupertino, CA 95014

 
 
 

Sleep in ksh script ignored until the sleep time expires

Post by Donn Ca » Tue, 29 Aug 1995 04:00:00


| I'm trying to write a ksh script that traps certain signals (HUP,
| INT, QUIT, TERM).  For testing, I wrote the following script:
|
|   #!/bin/ksh
|   trap 'print Signal caught' HUP INT QUIT TERM
|   while :; do
|      print "Sleeping ..."
|      sleep 30
|   done
...
| The script above didn't seem to trap the sent signal immediately.  But
| instead, completes the sleep (30 seconds) and then acknowledge the signal
| (by invoking the command specified after the "trap" built-in command).

Same with sh.  I believe that this one will work better:

    trap 'echo ouch; kill $child' 1 2 3 15
    while :
    do
        echo sleeping
        sleep 30 &
        child=$!
        wait
    done

The kill just keeps the sleeps from piling up.  The shell responds
immediately because the interrupted command (wait) is a builtin.

        Donn Cave, University Computing Service, University of Washington


 
 
 

Sleep in ksh script ignored until the sleep time expires

Post by Kurt Mad » Wed, 30 Aug 1995 04:00:00


: Hi,

: I'm trying to write a ksh script that traps certain signals (HUP,
: INT, QUIT, TERM).  For testing, I wrote the following script:

:    #!/bin/ksh
:    trap 'print Signal caught' HUP INT QUIT TERM
:    while :; do
:       print "Sleeping ..."
:       sleep 30
:    done

: I ran the script and opened another terminal session and issued a 'kill'
: command followed by the PID # of the above script (say 11980):
:    $ kill -INT 11980

: The script above didn't seem to trap the sent signal immediately.  But
: instead, completes the sleep (30 seconds) and then acknowledge the signal
: (by invoking the command specified after the "trap" built-in command).

: I posted a question to IBM, but the AIX Support person was unable to
: duplicate the situation.  He indicated during his testing that the signal
: was caught by the "trap" command immediately without waiting for the sleep
: to expire when he sent a signal using 'kill' to the PID of the ksh script.

: The script, however, did get interrupted immediately if I type Ctrl-C
: on the same terminal session running the script -- but this won't help me.

: Has anyone seen this problem?  Any known solution?

Funny, I am using the same thing as a "feature", i.e., I send a signal
to a process, and it will take action only after a subprocess has been
completed, as you observed also. If I want to send a signal to a
subprocess of the script, I have to send it to its own PID.  It
behaves in this way in bash, on AIX and on OSF/1. I'm not an expert,
but this behaviour makes sense to me.

Kurt
----
Kurt Mader                                   http://www.cecam.fr/~mader/

Atomique et Moleculaire (CECAM)
Ecole Normale Superieure de Lyon                  phone: +33 72 72 86 31
46, Allee d'Italie, 69364 Lyon cedex 07             fax: +33 72 72 86 36

 
 
 

Sleep in ksh script ignored until the sleep time expires

Post by King Oin » Wed, 30 Aug 1995 04:00:00



: : I'm trying to write a ksh script that traps certain signals (HUP,
: : INT, QUIT, TERM).  For testing, I wrote the following script:

: :    #!/bin/ksh
: :    trap 'print Signal caught' HUP INT QUIT TERM
: :    while :; do
: :       print "Sleeping ..."
: :       sleep 30
: :    done

: : I ran the script and opened another terminal session and issued a 'kill'
: : command followed by the PID # of the above script (say 11980):
: :    $ kill -INT 11980

That's ok!
you've sent a signal to
(speak with me :-)
  O  N  E
process.
So,
this process will get it!

: : The script, however, did get interrupted immediately if I type Ctrl-C
: : on the same terminal session running the script -- but this won't help me.

If you type Ctrl-C or DEL or any INT character you like
the line discipline of your controlling terminal will change
this input into a signal INT!
This signal is sent to ALL members of your foreground process group!

It is a significant difference between ^C and kill -INT!

: : Has anyone seen this problem?  Any known solution?

A solution for this problem:
Your ksh script must run in an own process group.
(Not so easy to achieve this situation using plain shell :-(
Find out the process group id!
Send an INT signal to this process group!
kill -INT -PGID
e.g.
kill -INT -567

your ksh script and all members of this process group will receive
a signal
and will perhaps end

--
      _/_/_/  _/ _/    _/
    _/    _/ _/ _/_/  _/
   _/    _/ _/ _/  _/_/
   _/_/_/  _/ _/    _/

 
 
 

Sleep in ksh script ignored until the sleep time expires

Post by Hemant Sh » Wed, 30 Aug 1995 04:00:00


->Hi,

->I'm trying to write a ksh script that traps certain signals (HUP,
->INT, QUIT, TERM).  For testing, I wrote the following script:

->   #!/bin/ksh
->   trap 'print Signal caught' HUP INT QUIT TERM
->   while :; do
->      print "Sleeping ..."
->      sleep 30
->   done

->I ran the script and opened another terminal session and issued a 'kill'
->command followed by the PID # of the above script (say 11980):
->   $ kill -INT 11980

->The script above didn't seem to trap the sent signal immediately.  But
->instead, completes the sleep (30 seconds) and then acknowledge the signal
->(by invoking the command specified after the "trap" built-in command).

   I have had same problem. It seems that ksh queues all the signals
   until the child process dies. In the above case it will queue the signals
   until "sleep 30" dies. So, at most you will have to wait for 30 seconds
   before your shell ends.

--
                                                             \\|//
                                                             (O-O)
---------------------------------------------------------oOO--(_)--OOo-----
Hemant Shah                      |  I haven't lost my mind,
LIDP, Inc.                       |  it's backed up on tape somewhere.
Voice: (708) 960 0133 Ext: 64    |
Fax: (708) 960 0717              |

WWW: http://www.xnet.com/~shah   | have their own.
--------------------------------------------------------------------------