tags:

views:

131

answers:

5

I want a Perl script to check a certain PID every couple of minutes and then kill the process. How do I wait those couple of minutes? Thanks.

+3  A: 

You're looking for sleep($numSeconds);

So to wait 2 minutes, you would execute sleep(120);

perldoc -f sleep

RC
+5  A: 

sleep (n); where n is the number of seconds you want to sleep.

MJB
+4  A: 

Use sleep(). The select() builtin can provide better granularity:

select(undef, undef, undef, 0.25);  #sleep of 250 milliseconds
eugene y
usleep is better for fine control. Select will (and does) work, but its really supposed to be used for networking.
Chris Huang-Leaver
The question says "couple of minutes".
Kinopiko
+1. select avoids SIGALRM muckery, as documented under perlfunc/sleep.
pilcrow
Time::HiRes redefines `sleep` in one that accepts fractional seconds.
Steve Schnepp
@Chris Huang-Leaver: the quoted line with `select` comes right ouf of the Perl documentation (`perldoc -f select`). This is official Perl's way to sleep for less than a second. Other bells and whistles are implemented in the optional `Time::HiRes` module.
Dummy00001
+1  A: 

or you could try usleep(microseconds) if a whole second is too long. sleep(0) simply yields to the OS, (discarding what time your process has left on the scheduler). Note that all these functions you ask for the minimum amount of time you want to sleep. The time may be slightly longer on a heavily loaded system.

sleep and usleep are C functions, although Perl, python etc. have functions which call the underling C ones. usleep man page, BSD version, (others are available, try Google)

Chris Huang-Leaver
+1 Don't know why you got a downvote. `usleep` isn't my preferred approach, but it certainly works.
pilcrow
@pilcrow, Chris: `usleep` is not available in vanilla Perl. And there is no mention of Time::HiRes which is de facto standard module for the purpose. That's why the response got down voted.
Dummy00001
@pilcrow, Chris: and BTW/FYI, `usleep` even as a syscall is now deprecated (since POSIXv6/SUSv3) in favor of `nanosleep`.
Dummy00001
@Dummy00001, thanks for clarifying. Uncommented downvotes are frustrating, IMHO, robbing everyone of the chance for a good discussion and an education.
pilcrow
My bad, didn't see the 'in Perl' bit, or did you edit the question title? :-)
Chris Huang-Leaver
+6  A: 

Crontab


If you want a Perl program to execute at a set time or time interval, then you might want to consider crontab or another scheduler.

Perl


If you want perform a wait from within the Perl script, you have a few easily deployable options.

System Calls

  1. sleep($n) system call where $n is a numeric value for seconds
  2. usleep($n) system call where $n is a numeric value for microseconds

Perl Modules

Time::HiRes provides a number of functions, some of which override the system calls. Some of the functions include: sleep(), usleep(), nanosleep(),alarm(), ualarm()

Unlike the system call to usleep(), the one packaged with Time::HiRes allows for sleeping more than a second.

use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep
                    clock_gettime clock_getres clock_nanosleep clock
                    stat );

Suggestion

You'll most likely want to fork your process so that your main program can continue working while that one process is in sleeping in the background.

vol7ron
+1 for nanosleep(), first Time::HiRes answer, and usleep differences
Armando
Because it's minutes, the best idea is to not tie up any more resources than need be and just use a scheduler.
vol7ron