Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Trap Computer Shutdown Signal

  1. #11
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Trap Computer Shutdown Signal

    And kill -TERM works as you expect? Does it behave the same way when you log out without shutting down, or does it continue running while you're logged out?

  2. #12
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Trap Computer Shutdown Signal

    Quote Originally Posted by ov3rcl0ck View Post
    Shell scripts are too high level to catch SIGTERM or SIGKILL. Maybe theres a way to do it, but I don't think trapping a signal will do it, seeing as the signal is sent to BASH/shell rather than your script. Unless BASH will relay this information to a shell script, which I don't think it does.
    Wrong!

    Here's an example script where SIGTERM is caught:
    Code:
    #!/bin/bash
    
    
    trap dothis SIGTERM
    
    
    function dothis()
    {
       echo "Revert to original wallpaper here."
       exit 0
    }
    
    while [ true ]
    do
       sleep 2
    done
    Run this script, then after referencing the PID of the script, issue a SIGTERM signal to it with something like:
    Code:
    kill -15 <pid>
    
    # or
    
    kill -s SIGTERM <pid>
    To get a list of all of the signals that are recognized by Bash, run
    Code:
    trap -l
    Obviously, as mentioned earlier, SIGKILL cannot be intercepted.

  3. #13
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Trap Computer Shutdown Signal

    On a suspicion I googled traps and loops and found this page: http://tldp.org/LDP/Bash-Beginners-G...ect_12_02.html
    It seems you need to add a wait statement if you want traps to work with loops, now I know what exactly the problem is it should be easy to fix it, so I'll mark this thread [SOLVED].

    Thanks for all the help!

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •