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.
You're looking for sleep($numSeconds);
So to wait 2 minutes, you would execute sleep(120);
Use sleep(). The select() builtin can provide better granularity:
select(undef, undef, undef, 0.25); #sleep of 250 milliseconds
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)
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
sleep($n)
system call where $n is a numeric value for secondsusleep($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.