user1539348
user1539348

Reputation: 513

How to manual wake a perl sleep function?

I was wondering if it was possible to manually wake a perl script that has gone to sleep. Basically I set my script to sleep for say an hour after performing something, and 10 minutes later I realize I want it to run again.

The platform I'm running on is linux, with a tcsh shell.

Again, Thanks for any help

Upvotes: 1

Views: 1498

Answers (1)

mob
mob

Reputation: 118685

On many systems, system calls like sleep can be interrupted with signals. On such an interruption, sleep will return the number of seconds that it actually slept for and set $!. Of course you have to use a signal that will allow your script to continue. If you haven't set any specific signal handler, a combination of SIGSTOP and SIGCONT will do the trick, too.

Example:

$ perl -e 'print "slept for ", sleep 1000, " seconds.\n";' &
[2] 6220
$ kill -STOP 6220
$ kill -CONT 6220
slept for 8 seconds[2]+  Stopped     perl -e ...

Another example (trivial SIGUSR1 handler):

$ perl -e '$SIG{USR1}=sub{}; print "slept for ", sleep 1000, "s.\n"' &
[1] 2419
$ kill -USR1 2419
slept for 4s

Upvotes: 5

Related Questions