获取一个 Unix 脚本,每次都在完全相同的时间运行
我正在编写一个脚本来捕获系统上的磁盘使用情况(是的,我知道有软件可以做到这一点)。 出于数据库报告的目的,我希望数据点之间的间隔尽可能相等。 例如,如果我每 10 分钟轮询一次磁盘使用情况,我希望每个数据点都是 YYYY-MM-DD HH:[0-5]0:00。 如果我每 5 分钟轮询一次,则为 YYYY-MM-DD HH:[0-5][05]:00。
如果我有一个 ksh 脚本(甚至是 Perl 脚本)来捕获磁盘使用情况,我怎样才能让脚本处于活动状态并在拍摄快照之前等待下一个“轮询时间”,然后休眠正确的秒数直到下一个“投票时间”。 如果我每 5 分钟轮询一次,并且时间是 11:42:00,那么我想休眠 180 秒,以便它将在 11:45:00 拍摄一张快照 - 然后休眠 5 分钟,以便它将拍摄另一个快照11:50:00。
我写了一种方法,如果我的轮询时间是每 10 分钟一次,则该方法有效,但如果我将轮询时间更改为不同的数字,则它不起作用。 我希望投票时间能够灵活一些。
我更喜欢在 shell 脚本中执行此操作,但如果代码太多,Perl 也可以。
关于如何实现这一目标有什么想法吗?
提前致谢!
Brian
编辑:哇 - 我遗漏了一个非常重要的部分 - cron 被禁用,所以我将无法使用 cron 来完成此任务。 我对所有给出这个答案的人感到非常抱歉,因为是的,如果我可以使用 cron,这是做我想做的事情的完美方式。
我将使用我们的调度程序在每天午夜之前启动我的脚本,并且我希望脚本能够处理在确切的“轮询时间”运行、在中间睡眠并在午夜退出。
再次,我很抱歉没有对 crontab 进行澄清。
I am writing a script to capture disk usage on a system (yes, I know there is software that can do this). For database reporting purposes, I want the interval between data points to be as equal as possible. For example, if I am polling disk usage every 10 minutes, I want every data point to be YYYY-MM-DD HH:[0-5]0:00. If I'm am polling every 5 minutes, it would be YYYY-MM-DD HH:[0-5][05]:00.
If I have a ksh script (or even a Perl script) to capture the disk usage, how can I let the script come active and wait for the next "Poll time" before taking a snapshot, and then sleep for the correct number of seconds until the next "Poll time". If I am polling every 5 minutes, and it is 11:42:00, then I want to sleep for 180 seconds so it will take a snapshot at 11:45:00 - and then sleep for 5 minutes so it will take another snapshot at 11:50:00.
I wrote a way that works if my poll time is every 10 minutes, but if I change the poll time to a different number, it doesn't work. I would like it to be flexible on the poll time.
I prefer to do this in shell script, but if it is way too much code, Perl would be fine too.
Any ideas on how to accomplish this?
Thanks in advance!
Brian
EDIT: Wow - I left out a pretty important part - that cron is disabled, so I will not be able to use cron for this task. I am very sorry to all the people who gave that as an answer, because yes, that is the perfect way to do what I wanted, if I could use cron.
I will be using our scheduler to kick off my script right before midnight every day, and I want the script to handle running at the exact "poll times", sleeping in between, and exiting at midnight.
Again, I'm very sorry for not clarifying on crontabs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
cron 会完成这项工作。
http://en.wikipedia.org/wiki/Cron
只需将其配置为运行您的 ksh在您需要的时候编写脚本,然后就完成了
cron will do the job.
http://en.wikipedia.org/wiki/Cron
Just configure it to run your ksh script at the times you need and you are done
您可能需要考虑使用
cron
。 这正是它的用途。You might want to consider using
cron
. This is exactly what it was made for.如果我这样做,我将使用系统调度程序(
cron
或其他)来安排我的程序每 180 秒运行一次。编辑:我可能误解了您的请求。 您是否正在寻找更多以下内容? (我怀疑这里有一两个错误):
另一个编辑:删除对 Time::Local 的依赖(但现在我怀疑有更多错误;-)
If I were doing this, I would use the system scheduler (
cron
or something else) to schedule my program to run every 180 seconds.EDIT: I might have misunderstood your request. Are you looking more for something along the following lines? (I suspect there is a bug or two here):
ANOTHER EDIT: Remove dependency on Time::Local (but now I suspect more bugs ;-)
让它休眠很短的时间,<=1秒,并每次检查轮询时间是否到达。 增量处理器的使用可以忽略不计。
编辑:如果您知道将使用什么时间间隔并且不打算经常更改,那么 cron 就很好。 但如果您经常更改间隔,请考虑连续运行的脚本和较短的睡眠时间。
Have it sleep for a very short time, <=1 sec, and check each time whether poll time has arrived. Incremental processor use will be negligible.
Edit: cron is fine if you know what interval you will use and don't intend to change frequently. But if you change intervals often, consider a continuously running script w/ short sleep time.
根据您的时间分辨率需要的细粒度,可能需要编写脚本守护程序样式。 while(1) 启动一次并执行程序内部的逻辑(您可以每秒检查一次,直到再次运行)。
如果您需要,Perl 的 Time::HiRes 允许非常细的粒度。
Depending on how fine grained your time resolution needs to be, there may be a need to write your script daemon style. Start it once, while(1) and do the logic inside the program (you can check every second until it's time to run again).
Perl's Time::HiRes allows very fine granularity if you need it.