大网数据广告活动
服务器运维问答

run cron job only if load not too high

· 52CE 我爱测

I need to synchronize two systems, so the producers list new entities to be created on the consumer app. It has no realtime need (mostly for datamining, so being a few hours behind isn't an issue), but it's important that this ta…

问题原文

原帖链接 · Xavier · CC BY-SA 3.0

I need to synchronize two systems, so the producers list new entities to be created on the consumer app. It has no realtime need (mostly for datamining, so being a few hours behind isn't an issue), but it's important that this task doesn't run when the server is already busy serving real users.

I have tried to run the sync service with a lower priority (nice -n19) but for what I saw, it can still hurt performances, as if this sync service runs a big database query, it's still going to increase the load on the database process and on the complete machine

is there a way to test the load and simply exit if it's too high (say above 2)?

Ideally, having some kind of alert if the cron isn't processing for too long because the load never goes below 2.

Is this a good approach or am I creating more problems that I'm solving?

社区高分解答

原帖链接 · shodanshok · CC BY-SA 4.0

Simply wrap you script inside a testing condition. For example:

#!/bin/bash
load=`grep -o "^[0-9]*" /proc/loadavg`
if [ $load -gt 2 ]; then
    exit 1
fi
### Call you script here ###

The above script will check the 1-minute load average and, if greater than 2, it will exit. On the other side, if the load average is less or equal to 2, it will continue.

More complex tests can be written, but the concept is the same: use a wrapper script/function and then call your custom sync script.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-07-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。