Click here to Skip to main content
15,881,615 members
Articles / Programming Languages / C++

Suspend Computer on Idle if CPU Usage is Low (Windows Tip)

Rate me:
Please Sign up or sign in to vote.
4.57/5 (5 votes)
22 May 2010Public Domain4 min read 31.8K   608   15   5
A 3-click package to suspend your computer when idle but only if CPU usage is low

Introduction

I like suspending my computer when I don't use it. I save power, and the room with the computer does not get warm. I would also like the computer to once in a while be able to do computations overnight. Unfortunately, the standard Windows suspension does not take CPU usage into account, so I would have to turn off standby every time I want the computer to do overnight computations.

About the Solution

I found that the easiest solution is to split the task of sending the system to standby into three steps rather than composing a single script or program to do the job:

  1. Identify that the computer is idle (no user inputs)
  2. Identify that the CPU usage is low, and perform a
  3. Suspension of the system

Task 1 is solved using the Windows Scheduler; a task can be scheduled to be executed after some specified idle time. The scheduler can schedule tasks using command line options only.

Next, I needed a program to do a conditional execution of the third and last process. As I didn't find any standard solution for this, I chose to modify another project which gets the CPU usage.

Finally, the computer should be sent to standby. Sending the system to standby using the command line is apparently easiest done with PsShutdown.exe, which is a part of PsTools created and maintained by Microsoft.

Set Up the Package

To set up the package, you will need to download and extract suspend-on-idle.zip to some appropriate folder on your computer. You will also need PsTools since the license agreement specifies that I cannot give you a copy of PsShutdown.exe along with my own solution. All you need from PsTools is PsShutdown, and you will need to copy it to the same folder as the rest of the package.

To make the solution work with your setup, open/edit activate.bat and look for the lines:

set percentLimit=10
set timeInMinutes=30
set adminaccount=Administrator

The three options let you choose:

  • Below what CPU usage limit (%) should the computer go to standby.
  • How long should there be no user inputs prior to checking the CPU usage.
  • Which account should perform the tasks. Preferably, you should use an account with admin rights such that the solution works also when other users or perhaps no users are logged in.

At this point, all you need to do is close the file and double click it (remember to type in your password correctly). To disable the automated suspend, simply run deactivate.bat.

If you have no interest in how the different parts work, then there is no need to continue from here.

A Few Details about activate.bat

Scheduling tasks from the command line is done using schtasks.exe such that the solution is easy to roll out to various systems. There are a variety of options available for the task scheduler but only a subset is needed. The following are used:

Option Description
/Create Specify to create a task
/RU "Administrator" Specify that the account Administrator is used for execution
/SC ONIDLE Specify that the execution condition is "On idle"
/I 30 Idle time before execution
/TN "Suspend on cpu-idle Also give the task a name
/TR "C:\myfolder\cpu-checker.exe 75 PsShutdown.exe -d -t 0" What to execute

Hence the total command line in this example will be:

SCHTASKS.exe /Create /RU "Administrator" /SC ONIDLE /I 30 /TN 
  "Suspend on cpu-idle" /TR "C:\myfolder\cpu-checker.exe 75 
  PsShutdown.exe -d -t 0"

In order to make it easy to distribute, the command line is embedded in a bat file such that the current folder is inserted automatically and the options (idle time, CPU limit, and execution account) are easy to identify. The important thing to notice is %CD%, which retrieves the current directory. For some reason, the execution path is not always set correctly for the task. This is the reason that the full path to PsShotdown.exe is given rather than the relative:

set percentLimit=10
set timeInMinutes=30
set adminaccount=Administrator

set folder=%CD%
set execute=%folder%\PsShutdown.exe -d -t 0
set cpuchecker=cpu-checker.exe %percentLimit% %execute%

set schexe=%folder%\%cpuchecker% 

SCHTASKS.exe /Create /RU "%adminaccount%" /SC ONIDLE /I 
  %timeInMinutes% /TN "Suspend on cpu-idle" /TR "%schexe%"

I was inspired by the last line of the tip found on this external site.

cpu-checker.exe

The program is pretty much a copy of the project Get CPU Usage with GetSystemTimes, so I won't go into the details about how to get the actual CPU usage. I assume that the initial program is correct and further that it works for multi-core CPUs. The largest difference is that CPU checker takes two arguments: the first argument is the usage limit, the second argument (rather, the rest of the command line) is what to execute if the CPU usage is lower than the given limit.

C++
int throttle_limit_in_percent = atoi(argv[1]);
process = cpu.GetUsage( &sys, &upTime );    //first is ALWAYS 0!
Sleep(200); //more than 100
process = cpu.GetUsage( &sys, &upTime );    //this one counts =)

if(sys < throttle_limit_in_percent){
  std::stringstream s;
  for(int i = 2; i < argc; i++){
    s<<" "<<argv[i];
  }

  std::string str = s.str();
  system(&str[0]);
}

Example of usage: Execute program.exe with the additional argument -someoption if the current CPU usage is less than 15%:

cpu-checker.exe 15 program.exe -someoption

PsShutdown.exe

The PStools package is managed by Microsoft. The primary motivation to use PsShutdown rather than the standard shutdown.exe which comes with NT based versions of Windows is that shutdown.exe, for some reason, cannot put a computer into standby mode if hibernate is enabled. The switches used for PsShutdown are -d for suspend/standby and -t 0 for execute after 0 seconds.

PsShutdown.exe -d -t 0

A full guide to use PsShutdown can be found on this page.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAdmin vs Coder Pin
johannesnestler21-May-10 0:16
johannesnestler21-May-10 0:16 
GeneralRe: Admin vs Coder Pin
Morten Gorm Madsen21-May-10 10:04
Morten Gorm Madsen21-May-10 10:04 
GeneralRe: Admin vs Coder Pin
johannesnestler24-May-10 21:48
johannesnestler24-May-10 21:48 
GeneralMessage Closed Pin
20-May-10 12:48
_beauw_20-May-10 12:48 
Message Closed
GeneralRe: My Vote of 4 Pin
Morten Gorm Madsen21-May-10 10:13
Morten Gorm Madsen21-May-10 10:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.