#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    11
    Rep Power
    0

    Signal handling help


    I have been working on a Linux programming book and came to an example on signal handling that gave me a hangup. I checked to make sure I copied it alright so I don't believe that's it. Here's the example:
    Code:
    #!/bin/sh
    
    trap 'rm -f /temp/my_tmp_file_$$' INT
    echo creating file /tmp/my_temp_file_$$
    date > /tmp/my_temp_file_$$
    echo "Press interrupt (CTRL-C) to interrup ...."
    while [ -f /tmp/my_tmp_file_$$ ]; do
            echo File Exists
            sleep 1
    done
    echo The file no longer exists
    
    [highlight]trap INT
    echo creating file /tmp/my_tmp_file_$$
    date > /tmp/my_temp_file_$$
    
    echo "press interrupt to interrupt ...."
    while [ -f /tmp/my_temp_file_$$ ]; do
            echo File Exists
            sleep 1
    done
    
    echo we never get here
    exit 0
    Which gives me the output:
    Code:
    creating file /tmp/my_temp_file_18058
    Press interrupt (CTRL-C) to interrup ....
    The file no longer exists
    trap: usage: trap [-lp] [[arg] signal_spec ...]
    I'm guessing it's in the trap statement syntax. Perhaps outdated usage?
    Thanks for any help.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    I suspect it is because the SIGINT trap having been 'fired' once is no longer active and hence cannot be reset by the 'simple' use of trap INT.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by SimonJM
    I suspect it is because the SIGINT trap having been 'fired' once is no longer active and hence cannot be reset by the 'simple' use of trap INT.
    Wouldn't that mean I would have to press ctrl-c at least once? The output I gave happens immediately after running and then exits. It seems like it only executes the first section of code before exiting on its own. Perhaps the trouble is in creating the file, exiting the loop before the interrupt?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    11
    Rep Power
    0
    Sorry guys. Problems was just a few mixups between tmp and temp. Yet another example of thoroughly checking the code. After that the file does run as expected.

IMN logo majestic logo threadwatch logo seochat tools logo