Windows Tip: Launching a low priority process

ITworld.com |  Operating Systems 3 comments

Send your Windows question to Mitch today!


See other Windows tips


A friend of mine who is a petroleum engineer recently told me about a problem he was having. He was launching a series of batch jobs (big simulation jobs) on his Windows XP workstation from the command line by running a batch file that contained a series of commands like job5.exe -i input.dat, and his problem was that these jobs were hogging so much of his machine's processing power that foreground tasks became unresponsive. Once these processes were running however, he could open Task Manager and change their priority to Low, and then he could work normally on the machine while the jobs ran in the background. He was wondering if there was a way to start an application with Low priority instead of Normal.



It turns out there are several solutions to this problem. A simple approach is to use the start command to launch each job with Low priority as follows:

start /low /b job5.exe -i input.dat

The /b switch causes the job to run in the current command window but with Low priority. Another solution would be:

start "Job #1" /low job5.exe -i input.dat

This runs the job in a separate command window with "Job #1" in the title bar to distinguish it from the spawning command window in which the batch file itself runs. Still another solution is to do this:

start "Low cmd" /low cmd.exe

This opens a new command window where the cmd.exe process has Low priority and the window's title bar displays "Low cmd." Once this new command window is open, you can run your batch file in it and all jobs spawned by the batch file will run with Low priority also. Still another variation of this is:

start /low /b cmd.exe

This runs a second cmd.exe process with Low priority within your visible Normal priority command window, and again running the batch file will launch all your jobs with Low priority.



Batch file language can be useful, but what if you wanted to select the priority of a job at run time? That's where VBScript provides a more flexible solution. Here's a simple script named LaunchProcess.vbs that I wrote to do this:

Const LOW = 64
Const BELOW_NORMAL = 16384
Const NORMAL = 32
Const ABOVE_NORMAL = 32768
Const HIGH = 128

set args = WScript.Arguments
if args.Count = 0 then
   WScript.Echo "Usage: CScript Launch.vbs JOB PRIORITY"
   WScript.Echo "where JOB is the application or batch file including its path"   
   WScript.Echo "and PRIORITY is HIGH | ABOVE_NORMAL | NORMAL| BELOW_NORMAL | LOW"
   WScript.Echo ""
   WScript.Echo "Example: Cscript Launch.vbs C:\Windows\Notepad.exe BELOW_NORMAL"
   WScript.Quit 1
end if

strJob = args.Item(0)
strPriority = args.Item(1)

if strPriority = "LOW" then
   intPriority = LOW
elseif strPriority = "BELOW_NORMAL" then
   intPriority = BELOW_NORMAL
elseif strPriority = "NORMAL" then
   intPriority = NORMAL 
elseif strPriority = "ABOVE_NORMAL" then
   intPriority = ABOVE_NORMAL
elseif strPriority = "HIGH" then
   intPriority = HIGH 
else
   intPriority = NORMAL 
   strPriority = "NORMAL"
end if

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")

Set objConfig = objStartup.SpawnInstance_
objConfig.PriorityClass = intPriority
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
objProcess.Create strJob, Null, objConfig, intProcessID

Wscript.Echo strJob & " is running with " & strPriority & " priority."

To use this script, type something like this from the command line:

Cscript LaunchProcess.vbs C:\Windows\Notepad.exe BELOW_NORMAL

This will start Notepad with Below Normal priority, which you can verify using Task Manager.

 

3 comments

    Anonymous 2 years ago
    Please show an example of how to launch a command in low priority where the command includes parameters that must be specified in quotes (e.g. parameters that are filenames with spaces)
    Anonymous 2 years ago in reply to Anonymous
    OK, the solution to the first poster's problem is to put some text (it doesn't matter what) in quotes before any of the other quoted stuff. As good a place as any to put it is right after the START clause.The reason is that the START command automatically assumes the first quoted string is the name you want to give the new window. Regardless of whatever the first quoted string is actually supposed to do.Bad design decision on Microsoft's part. They should have made it so that the name of the new window follows an actual flag. But they didn't. So we have to fix their mistake.
    Anonymous 2 years ago in reply to Anonymous
    Yes please.

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      Operating SystemsWhite Papers & Webcasts

      White Paper

      Microsoft Enterprise Agreement Program Overview

      Discover how flexible the Microsoft Enterprise Agreement Program is to help you build the right software solution agreement for your business. This paper highlights all the available options-from on-premise software and cloud service solutions, to payment options and enrollment programs, and more.

      White Paper

      Watson - A System Designed for Answers. The future of workload optimized systems design

      Watson is a workload optimized system designed for complex analytics, made possible by integrating massively parallel POWER7 processors and DeepQA technology. Read the white paper about Watson's workload optimized system design.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question