Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bash script (on osx) running continuously, it currently traps and handles a terminate like so

trap onshutdown TERM

how can I make trap also handle suspend/resume like

trap onsuspend ?

trap onresume ?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

You're looking for signals SIGTSTP and SIGCONT. Try this:

trap onsuspend TSTP
trap onresume  CONT
share|improve this answer
1  
But note that you can't catch SIGSTOP, which is the SIGKILL to SIGTSTP's SIGTERM. (In other words, SIGTERM is “please die”, SIGKILL is “you're dead”, SIGTSTP is “please suspend” and SIGSTOP is “you're suspended”.) –  Gilles Apr 17 '11 at 12:26
    
@Gilles yes, but no normal program will send SIGCONT. It also doesn't allow terminal programs to clean up the screen because it can't be handled. I have never seen it except when I have sent it myself. I have on the other hand seen programs normally in the way of shutdown scripts send SIGKILL. –  penguin359 Apr 17 '11 at 17:16
    
THe above solution works correctly if I send kill -SIGTSTP PID or SIGCONT. however sigtstp and sigcont doesnt seem to be sent when for example you close the mbp lid and open it later. do you know what signals you should be trapping for those? –  Pradeep Apr 17 '11 at 19:26
1  
Laptop suspend/hibernate is really a system-wide event. Individual processes don't get notified normally. There might be a udev/hal/dbus event you can monitor, but it's a lot more sophisticated than catching a process signal. You also may not have time to do anything, I don't have much experience in this area. –  penguin359 Apr 17 '11 at 21:42
    
@Gilles @Pradeep I meant SIGSTOP. I've never seen any process actively send that signal, only SIGTSTP. –  penguin359 Apr 17 '11 at 21:46
show 1 more comment

Since the program is simply suspended and not reliably told, I would instead set up a named pipe and spawn a sentinel script.

It would simply loop every 5 or 15 minutes writing the current time stamp to the named pipe and then sleep.

You could then read from that pipe and do the math between reads - if the time jumps more than one or two pings, then you have slept.

Depending on how accurate you need the times, you could then tail the /private/var/log/system.log file (and potentially /private/var/log/system.0.log.gz) for the last sleep / wake times logged by the kernel.

It's going to be more work than relying on signals. You could also have the helper run itself and send whatever signal you want to your script.

Six of one, half dozen of the other.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.