Windows中类似于“nice”的命令是什么?

80

有没有Windows版本的Unix命令nice的等效工具?

我特别需要一些可以在命令行中使用的内容,而不是任务管理器中的“设置优先级”菜单。

我在谷歌上的搜索尝试被那些无法想出更好形容词的人挫败了。

4个回答

71

如果你想在启动进程时设置优先级,可以使用内置的START命令:

START ["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/WAIT] [/B] [command/program] [parameters]

使用“低”和“低于正常”选项来设置启动命令/程序的优先级。看起来是最直接的解决方案。无需下载或编写脚本。但其他解决方案可能适用于已运行的进程。


9
为使start更像nice,使用/WAIT/B选项让终端输出到同一窗口。 - Albert Armea
然而,这似乎完全删除了您的命令历史记录(包括使用箭头键和按F7键)。 - Albert Armea
3
这仍然没有回答如何更改正在运行的进程的优先级 - 目前duane(通过VBScript)的答案是最好的答案。 - Simon Sobisch
如果您尝试运行类似于 start /low "C:\Program Files\myprog.exe" param1 param2 的命令,您将收到错误消息“Windows无法找到 'param1'”。解决方法是将“C:\ Program Files \ ...”替换为c:\ progra〜1 \ ..,不用引号(DOS目录名称)。 - Putnik
如何在bash中运行?对于start /LOW /B type ls,我收到一个弹窗提示:“找不到B:/”。 - jaques-sam

10
如果您使用PowerShell,您可以编写一个脚本来更改进程的优先级。我在Monad博客上找到了以下的PowerShell函数:
function set-ProcessPriority { 
    param($processName = $(throw "Enter process name"), $priority = "Normal")

    get-process -processname $processname | foreach { $_.PriorityClass = $priority }
    write-host "`"$($processName)`"'s priority is set to `"$($priority)`""
}

从PowerShell提示符下,您需要执行以下操作:

set-ProcessPriority SomeProcessName "High"

注意事项:
  • 不要包含“.exe”(可能对其他人来说很明显,但它会出现在任务管理器中,让我感到困惑)。
  • 优先级区分大小写(只有第一个字母是大写)。
- Raúl Salinas-Monteagudo

6
也许你可以考虑使用ProcessTamer,它可以根据你的设置自动降低或提高进程优先级。
我已经使用了两年。它非常简单但确实有效!

5

来自 http://techtasks.com/code/viewbookcode/567

# This code sets the priority of a process

# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Windows Server Cookbook" by Robbie Allen
# ISBN: 0-596-00633-0
# ---------------------------------------------------------------

use Win32::OLE;
$Win32::OLE::Warn = 3;

use constant NORMAL => 32;
use constant IDLE => 64;
use constant HIGH_PRIORITY => 128;
use constant REALTIME => 256;
use constant BELOW_NORMAL => 16384;
use constant ABOVE_NORMAL => 32768;

# ------ SCRIPT CONFIGURATION ------
$strComputer = '.';
$intPID = 2880; # set this to the PID of the target process
$intPriority = ABOVE_NORMAL; # Set this to one of the constants above
# ------ END CONFIGURATION ---------

print "Process PID: $intPID\n";

$objWMIProcess = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2:Win32_Process.Handle=\'' . $intPID . '\'');

print 'Process name: ' . $objWMIProcess->Name, "\n";

$intRC = $objWMIProcess->SetPriority($intPriority);

if ($intRC == 0) {
    print "Successfully set priority.\n";
}
else {
    print 'Could not set priority. Error code: ' . $intRC, "\n";
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接