19

Is there a way to configure a Windows service process (e.g. squidNT, SQL Server, etc) to start automatically with low priority without manually changing process priority via Task Manager?

2
  • Changing the priority of threads or processes is usually a bad idea, unless the code has been written to run at a different priority. Changing priorities adds "priority inversion" to the list of concurrency issues to avoid.
    – Richard
    Commented Sep 21, 2009 at 9:12
  • 4
    The OP did not ask if it was a good idea or not. The OP asked if it could be done, and how. -- Please remember to stay on topic. 😉 Commented May 21, 2019 at 21:42

5 Answers 5

11

The Service Control Manager, which handles starting / stopping services, doesn't have any mechanism (in any version of Windows heretofore) to specify the priority on processes it starts.

Since you can modify the priority on an already-running process, your best bet might be to use a tool to do that.

I'm not aware of a Microsoft command-line tool to modify process priority, but the "PV" command-line too, available at http://www.teamcti.com/pview/prcview.htm has a function to set priority.

pv -pb process-name.exe

That would set "process-name.exe" to "Below Normal" priority.

If you can live with the process starting out at "Normal" priority until you get around to changing it, you could do so with a script running as a "Scheduled Task" to fire off every-so-often (in case the service gets bounced).

It's a quick and dirty hack, but I owe a lot of my fortune in life to quick and dirty hacks that get the job done!

1
  • Maybe run a batch file to start the app and then set the process priority, with perhaps a short sleep in between, rather than running the app directly and then running another task to set the priority. Won't work for everything but worth a try. Commented Sep 21, 2009 at 2:38
5

Above answers didn't work for me. I ended creating the following powershell script:

ChangePriority.ps1

Get-WmiObject Win32_process -Filter 'name="ProcessName.exe"' | ForEach-Object {$_.SetPriority(128)}

And then I created a scheduled task that call it every 5 minutes. From Administrator prompt or batch script:

schtasks /Create /tn "Process Priority Change" /sc MINUTE /mo 5 /ru SYSTEM /tr "powershell -noprofile -executionpolicy bypass -file \"%cd%\ChangePriority.ps1\""

Attempts to use wmic as discussed here to make the scheduled task not depending on file failed. Similary using the powershell script: I was not able to just create a single command that just create a task with the inline script. Appreciated if you managed to do it and add another answer.

Values for priority in the powershell script can be found here[1].

[1] https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/setpriority-method-in-class-win32-process

3
  • Very useful. I chopped up a line with my poor ps skills to set the audio service containing service host process to realtime to prevent audio crackling during heavy cpu usage (bad drivers, I guess). Works great!
    – JasonXA
    Commented Dec 7, 2019 at 12:43
  • @JasonXA You're welcome. Appreciated if you can provide a one line version of it, which I couldn't
    – ceztko
    Commented Dec 8, 2019 at 14:22
  • Sure: PowerShell.exe -noprofile -executionpolicy bypass -command "& { Get-WmiObject Win32_service -Filter \"Name = 'AudioSrv'\" | ForEach { Get-WmiObject Win32_process -Filter \"ProcessId = $($_.ProcessId)\" } | ForEach-Object { $_.SetPriority(256) } }" I don't know how right it is... but it works. I haven't managed to make it work hidden, can work only under user w/ admin priv.
    – JasonXA
    Commented Dec 9, 2019 at 15:17
1

The following might also work. Edit the ImagePath registry key, and use the start command from cmd.exe:

START ["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/WAIT] [/B] [command/program]
      [parameters]
1
  • Unfortunately this doesn't work at all since the executable launched in ImagePath must respect the interface for Windows services, and start doesn't implement nor it won't route this to the executable being launched.
    – ceztko
    Commented Apr 14, 2019 at 12:23
0

I had to do that to make teamcity take less cpu. And, because teamcity is the only java app we are running, that solution works perfectly.

  1. Set service as startup "manual".
  2. Create a batch file, that will start your service, and lower the priority, like:

    net start tcbuildagent
    net start teamcity
    wmic process where name="java.exe" CALL setpriority "below normal"

  3. Create a "task scheduler" job, triggered at "windows startup", that will launch that batch file.

I configured the TaskScheduler trigger with a 1 minute delay, to not interfere with other services starting.

p.s. you can also set all your services startup as manual, and use that batch file to start all of them in the order you want! SQL, IIS, etc.

0

You could the following powershell script, put it in a scheduled task that runs at startup.

do {
    start-sleep -s 5
    $obj = Get-WmiObject Win32_process -Filter 'name="processname"'
} while ($obj -eq $null)
$obj.SetPriority(16384)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .