應用程式(尤其是大型 Java 和 C++ 應用程式)通常在 中顯示為多行htop
,每行都有單獨的 PID 和單獨的良好等級。此外,應用程式可以產生許多子進程(如中所示aptitude update
),因此我需要影響父進程(使新子進程具有新的優先權)和子進程(立即生效,而不是在子進程終止後)
如何將“renice”或“ionice”或“schedtool”應用於已啟動的大型應用程式?
答案1
1)取得最頂層進程的PID,並記住它。
2)取得具有記住的PID的PPID的所有進程,並記住它們的PID
3)重複步驟2,直到沒有新的PID。
4) 對於每個 PID,將指令套用到該進程。
用您最喜歡的語言應該不會太難。
答案2
我沒有完全可靠的解決方案,但在許多情況下,目標進程集將是一個進程組。如果$parent
是父進程的 pid,則以下命令列出群組中進程的 pid:
ps -eo pgrp:1=,pid:1= |sed -n "s/^$parent //p"
答案3
我知道這有點舊了,但由於這是我搜尋相同內容時出現的情況,所以我想我應該發布我的解決方案(也發佈在這個要點中,以防我更新它)
#!/usr/bin/env bash
# This can be run simply by passing it the outputs from pgrep:
# my_renice $(pgrep application)
#
# You may also want to use pgrep to find more complex
# processes based on arguments
# my_renice $(pgrep -f "bash.*$name")
function my_renice(){
newnice=10
pid=$1
# Return if pid not found
if [ -z $pid ]; then return; fi
# Renice pid right away in case we spawn more children
renice $newnice $pid
# Find children pids
children=$(pgrep -d ' ' -P $pid)
# Loop through children
for i in $children; do my_renice $i; done
}
答案4
sudo renice -n num $(pidof processname)