记录 cronjobs 的 cpu 时间、峰值内存等

发布于 2024-12-22 01:53:39 字数 451 浏览 3 评论 0原文

我在网络服务器上有相当多的 cronjobs,它们的运行程度各不相同。

似乎其中一些在测试数据库上需要意外数量的 CPU 周期。当然,我可以看到 16:30 的作业有哪些,但由于 cron 的数量,如果我有这样的日志,我的任务会容易得多:

Dec 19 02:13:09  /var/crons/cron1  [user cpu time used]  [system cpu time used]  [peak mem]  ..

是否有一个实用程序可以在 crontab 之前添加,例如so:

*/6 * * * * nice -n 18 getrusage?? /usr/bin/php /var/crons/cron1 > /var/log/something

也许与getrusage(2)有关?

I have a rather large number of cronjobs on a webserver that are ran with varying levels of niceness.

It appears that some of them are requiring an unexpected number of CPU cycles on the test database. Of course, I can see what jobs rant at 16:30, but due to the number of crons, my task would be much easier if I had a log like this:

Dec 19 02:13:09  /var/crons/cron1  [user cpu time used]  [system cpu time used]  [peak mem]  ..

Is there a utility that I could just add before the crontab, like so:

*/6 * * * * nice -n 18 getrusage?? /usr/bin/php /var/crons/cron1 > /var/log/something

Maybe something related to getrusage(2)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素罗衫 2024-12-29 01:53:39

实际上,您可以从 PHP 调用 getrusage,所以也许这个想法是可行的:不是从 crontab 调用 /usr/bin/php,而是调用一个包装器,在调用实际的 PHP 脚本之前和之后调用 getrusage:

#!/usr/bin/php
<?php
$fh = fopen("/var/log/cronusage.log", "a");
fputs($fh, "Executing " . $_SERVER['argv'][1] . "\n");
fputs($fh, "BEFORE\n");
foreach (getrusage() as $k => $v) {
    fputs($fh, "$k -> $v\n");
}
fclose($fh);

$_SERVER['argv'][0] = '/usr/bin/php';
// FIXME Attention args not escaped possible security vuln.
system(join(' ', $_SERVER['argv']));

$fh = fopen("/var/log/cronusage.log", "a");
fputs($fh, "AFTER\n");
foreach (getrusage() as $k => $v) {
    fputs($fh, "$k -> $v\n");
}
fputs($fh, "\n");
fclose($fh);
?>

它没有经过测试,只是一个想法。

time(1) 命令对于此目的也可能有用。

You can actually call getrusage from PHP, so maybe this idea is workable: instead of calling /usr/bin/php from the crontab, invoke a wrapper that calls getrusage before and after calling the actual PHP script:

#!/usr/bin/php
<?php
$fh = fopen("/var/log/cronusage.log", "a");
fputs($fh, "Executing " . $_SERVER['argv'][1] . "\n");
fputs($fh, "BEFORE\n");
foreach (getrusage() as $k => $v) {
    fputs($fh, "$k -> $v\n");
}
fclose($fh);

$_SERVER['argv'][0] = '/usr/bin/php';
// FIXME Attention args not escaped possible security vuln.
system(join(' ', $_SERVER['argv']));

$fh = fopen("/var/log/cronusage.log", "a");
fputs($fh, "AFTER\n");
foreach (getrusage() as $k => $v) {
    fputs($fh, "$k -> $v\n");
}
fputs($fh, "\n");
fclose($fh);
?>

It's not tested, just an idea.

The time(1) command might also be useful for this purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文