如何在 Linux 上的 Perl 中等待一段时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3186474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:13:13  来源:igfitidea点击:

How to wait for a certain period of time in Perl on Linux

linuxperl

提问by Chaggster

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.

我想要一个 Perl 脚本每隔几分钟检查一个特定的 PID,然后终止该进程。我如何等待那几分钟?谢谢。

回答by RC.

You're looking for sleep($numSeconds);

您正在寻找 sleep($numSeconds);

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

所以要等待 2 分钟,你会执行 sleep(120);

perldoc -f sleep

perldoc -f 睡眠

回答by MJB

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

sleep (n);其中 n 是您想要休眠的秒数。

回答by Eugene Yarmash

Use sleep():

使用sleep()

sleep(120);  # sleep for 120 seconds 

For delays of finer granularity than one second, you could use usleepfrom the Time::HiResmodule. You may also use Perl's four-argument version of select()leaving the first three arguments undefined:

对于比一秒更细粒度的延迟,您可以使用usleepfromTime::HiRes模块。你也可以使用 Perl 的四参数版本,select()不定义前三个参数:

select(undef, undef, undef, 0.25);  # sleep for 250 milliseconds

回答by Chris Huang-Leaver

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 minimumamount of time you want to sleep. The time may be slightly longer on a heavily loaded system.

或者您可以尝试usleep(microseconds)一整秒是否太长。 sleep(0)简单地屈服于操作系统,(丢弃您的进程在调度程序上剩余的时间)。请注意,所有这些功能都是您要求的最短睡眠时间。在负载较重的系统上,时间可能会稍长一些。

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)

sleep 和 usleep 是 C 函数,尽管 Perl、python 等具有调用底层 C 函数的函数。 usleep 手册页,BSD 版本,(其他可用,请尝试谷歌)

回答by vol7ron

Crontab

定时任务表



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

如果您希望 Perl 程序在设定的时间或时间间隔内执行,那么您可能需要考虑crontab或其他调度程序。

Perl

珀尔



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

如果您想从 Perl 脚本中执行等待,您有几个易于部署的选项。

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
  1. sleep($n)系统调用,其中 $n 是秒的数值
  2. usleep($n)系统调用,其中 $n 是微秒的数值

Perl Modules

Perl 模块

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

Time::HiRes提供了许多函数,其中一些会覆盖系统调用。部分功能包括:sleep(), usleep(), nanosleep(), alarm(),ualarm()

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

与对 的系统调用不同usleep(),与Time::HiRes打包的系统调用允许睡眠超过一秒。

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.

您很可能希望 fork 您的进程,以便您的主程序可以在该进程在后台休眠时继续工作。