我的PHP脚本如何判断服务器是否正忙?

时间:2022-11-28 07:14:04

I want to run a cron job that does cleanup that takes a lot of CPU and Mysql resources. I want it to run only if the server is not relatively busy.

我想运行一个执行清理的cron作业,它占用了大量的CPU和Mysql资源。我希望它只在服务器不是比较繁忙时运行。

What is the simplest way to determine that from PHP? (for example, is there a query that returns how many queries were done in the last minute? ...)

从PHP确定这个最简单的方法是什么? (例如,是否有一个查询返回在最后一分钟完成了多少次查询?...)

4 个解决方案

#1


12  

if (function_exists('sys_getloadavg')) {
    $load = sys_getloadavg();
    if ($load[0] > 80) {
       header('HTTP/1.1 503 Too busy, try again later');
       die('Server too busy. Please try again later.');
    }
}

Try to update this function to your needs

尝试根据您的需要更新此功能

#2


4  

On Linux you can get load from /proc/loadavg file.

在Linux上,您可以从/ proc / loadavg文件加载。

$load = split(' ',file_get_contents('/proc/loadavg'))
$loadAvg = $load[0]

#3


3  

If this is a Unix system, parse the output of uptime. This shows the CPU load which is generally considered to be a good measure of "busy." Anything near or over 1.0 means "completely busy."

如果这是一个Unix系统,解析正常运行时间的输出。这显示了CPU负载,通常被认为是“忙”的一个很好的衡量标准。接近或超过1.0的任何东西都意味着“完全忙碌”。

There are three CPU "load" times in that output giving the load average over 1, 5, and 15 minutes. Pick whichever makes sense for your script. Definition of "load" is here.

在该输出中有三个CPU“加载”时间,给出平均负载超过1,5和15分钟。选择适合您脚本的选项。 “加载”的定义在这里。

#4


2  

You can probably use the info in the Mysql List Processes function to see how many are active vs. sleeping, a decent indicator about the load on the DB.

您可以使用Mysql List Processes函数中的信息来查看有效数据与睡眠数量之间的关系,这是关于数据库负载的一个不错的指标。

If you're on Linux you can use the Sys GetLoadAvg function to see the overall system load.

如果您使用的是Linux,则可以使用Sys GetLoadAvg函数查看整个系统负载。

#1


12  

if (function_exists('sys_getloadavg')) {
    $load = sys_getloadavg();
    if ($load[0] > 80) {
       header('HTTP/1.1 503 Too busy, try again later');
       die('Server too busy. Please try again later.');
    }
}

Try to update this function to your needs

尝试根据您的需要更新此功能

#2


4  

On Linux you can get load from /proc/loadavg file.

在Linux上,您可以从/ proc / loadavg文件加载。

$load = split(' ',file_get_contents('/proc/loadavg'))
$loadAvg = $load[0]

#3


3  

If this is a Unix system, parse the output of uptime. This shows the CPU load which is generally considered to be a good measure of "busy." Anything near or over 1.0 means "completely busy."

如果这是一个Unix系统,解析正常运行时间的输出。这显示了CPU负载,通常被认为是“忙”的一个很好的衡量标准。接近或超过1.0的任何东西都意味着“完全忙碌”。

There are three CPU "load" times in that output giving the load average over 1, 5, and 15 minutes. Pick whichever makes sense for your script. Definition of "load" is here.

在该输出中有三个CPU“加载”时间,给出平均负载超过1,5和15分钟。选择适合您脚本的选项。 “加载”的定义在这里。

#4


2  

You can probably use the info in the Mysql List Processes function to see how many are active vs. sleeping, a decent indicator about the load on the DB.

您可以使用Mysql List Processes函数中的信息来查看有效数据与睡眠数量之间的关系,这是关于数据库负载的一个不错的指标。

If you're on Linux you can use the Sys GetLoadAvg function to see the overall system load.

如果您使用的是Linux,则可以使用Sys GetLoadAvg函数查看整个系统负载。