Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9127901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:23:31+00:00 2026-06-17T07:23:31+00:00

How can I run a cron job(bash script) only when CPU idle >50%? I

  • 0

How can I run a cron job(bash script) only when CPU idle >50%?

I can get cpu idle from TOP

top -b -d 00.10 -n 3 |grep ^Cpu
Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

my current setup is:

crontab
0,15,30,45 * * * * /usr/bin/php /home/user/batchprocess.php
# I could use a bash script here to call PHP, if it is a good solution.

And I have the PHP script check for CPU idle:

batchprocess.php
proc_nice(10);
// wait for CPU idle
do{
    $cpu_stat = exec('top -b -d 00.10 -n 3 |grep ^Cpu');    
    $tmp = stristr($cpu_stat,'%id',TRUE);
    $cpuidle = trim(substr($tmp,strrpos($tmp,',')+1));
}while($cpuidle<$min_cpuidle);
// do actual processing here

The problem with my current method is it starts the program regardless of CPU utilization. And the while loop running TOP feels not efficient. I wish it only starts when CPU idle>50

Some additional info:

  • Centos 6.2, PHP5.3

  • I have a few EC2 instances that never shuts down, so I want to utilize their processing power when idle. But never load the server heavily. (redundancy DB instance, Development instance, NAT instance)

  • I know EC2 auto scaling, spot instance. Just want to use the extra capacity.

  • the background job is image compression (CPU intensive, not much I/O or network).

Any suggestion is welcome. thanks in advance!


Based on the input below, I realized that “nice” is a better solution in my case. I should re-align my objective to minimize impact to the server instead of tracing CPU utilization.

So the new setup is:

crontab
0,15,30,45 * * * * nice -20 /usr/bin/php /home/user/batchprocess.php

And PHP script:

batchprocess.php
if ($cpuidle < 50) 
    exit(0);
// do actual processing here

I’ll test it and post back my finding.


report back: I have put this code through DEV/PRD, it works quite well. It does not solve the problem of TOCTOU but is good enough for now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:23:32+00:00Added an answer on June 17, 2026 at 7:23 am

    This is a typical case of TOCTOU – you check if the system is idle, and start your process – but after your check, as or before your process starts, something else caused another process in the system to kick off and you still load the system more than necessary.

    The “correct” way to do this is to give your process a low priority, using the nice command. By the way, your loop to check cpu usage will use 100% cpu, so it probably wouldn’t work unless THE FIRST time you check it’s idle.

    You already have a “proc_nice(10)”, so that should do the job. I don’t see any point in spending effort in identifying whether the system is busy or not.

    If you watned to, at suitable points in your code, you could do something like:

     if (check_cpu_usage() > 50%) sleep(1second); 
    

    But I’m not sure if that’s much use – if the system is busy, a “nice” process won’t receive much CPU-time, and thus won’t compete with other processes that run at higher priority.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a PHP script that can be run as a cron job
How can i run an cron job from php, and the cron to start
I can't seem to run a simple bash file as a cron job that
I have a script that I'm trying to run from cron. When I run
i have start.sh bash script that is running though CRON JOB on ubuntu server
Hi I want to run a cron job to call a PHP script on
I have a shell script that I can run successfully from the command line.
I would like to call a cron job from within a PHP script. I
I have a CakePHP script that should hopefully be run by a cron job.
I'm trying to run a scheduled cron job using a crontab from a linux

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.