Hi, I might be being a noob but reading the OP, aren't they wanting to call the
value arbitrarily? Meaning, e.g. an Ajax call in a web page could send a
request to find out the time remaining in the sleep.
I guess that the sleep (which will halt the script) needs to be invoked after
forking a decrementing counter? The decrementing counter will run
asynchronously and could be queried using Fork::Super bg_eval, but if you are
asking this question that might be a stretch (it's not something I've used).
As there is 'always more than one way to do it' I would use a fork and a file:
The simplest method, if it will do what you want would be to use an until loop
to count down and do the sleeping, but you would need to decide up front
whether you want to return a value (you could always write it to a file and
call the contents of the file?).
Need more info on what you want to do with it, but on a basic level, this will
work. It passes the sleep time value to countdown and forks that process so
the rest of the script can proceed. I put the actual 300 sec sleep at the
bottom, but if you cat countdown.txt at any point it will tell you how long is
left.
#!/usr/bin/perl -w
use strict;
my $sleep_timer;
my $count_amount=('10');
if ( ! fork() ) {
countdown($count_amount);
} #### Fork the counting process
sub countdown {
$sleep_timer = shift;
print_remaining($sleep_timer); ##I've put the printing in a routine
## as
we need it in two places
sleep 1; ### Do the first second sleep before decrementing the counter
### That way it will get all the way down to zero
until ($sleep_timer == '0') {
$sleep_timer--;
print_remaining($sleep_timer);###Pass the current count to our
printing sub
sleep 1;
}
}
sub print_remaining {
my $counter = shift;
open FH1 ,"+>countdown.txt";
print FH1 "$sleep_timer";
close FH1;
}
### The main part of the script will hold on until the time has
sleep 10;
print "I waited $count_amount seconds to tell you this";
BEWARE using forks that you have some failsafe in place to stop the script
being run multiple times, or the same sub will overwrite the counter file so
the number will start to jump around all over the place.
Hope thats useful.
Ed
On 16 Sep 2013, at 00:49, "John W. Krahn" <[email protected]> wrote:
> Shawn H Corey wrote:
>> On Sun, 15 Sep 2013 13:00:36 -0700
>> Unknown User<[email protected]> wrote:
>>
>>> If my perl script has a sleep for say 300 seconds, when the sleep is
>>> being run is there any way i can find the time remaining in the sleep
>>> say by sending a signal?
>>>
>>> Thanks,
>>
>> Not directly. You have to record the time before the sleep and then you
>> can measure how long the sleep lasted.
>>
>> my $started_sleep = time;
>> sleep 300;
>> my $time_asleep = time - $started_sleep;
>
> Or just:
>
> my $time_asleep = sleep 300;
>
>
>
> John
> --
> Any intelligent fool can make things bigger and
> more complex... It takes a touch of genius -
> and a lot of courage to move in the opposite
> direction. -- Albert Einstein
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/