tracker-miner-f、tracker-store、tracker-extract 和 dropbox 进程正在耗尽我的 CPU,它们使我的 CPU 以超过 100% 的速度运行,导致我的笔记本电脑过热。
我已经安装了 CPULIMIT,它可以很好地限制这些进程,但是,我无法让它自动启动。
我已经关注这但它不起作用,守护进程启动了,但我认为脚本创建时出现错误,它实际上并没有限制任何进程。
有没有一种方法可以减少 CPU 使用率,而不必每次开始新会话时都通过终端手动执行此操作?
Ubuntu 14.04
下面我发现的一个脚本只能部分运行并且不能正确检测 PID。
如果有人比我更了解,你可以检查一下这一行:
NEW_PIDS_COMMAND="top -b -n1 -c | grep -E '$BLACK_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
它是否正确?
#!/bin/bash
# ==============================================================
# CPU limit daemon - set PID's max. percentage CPU consumptions
# ==============================================================
# Variables
CPU_LIMIT=40 # Maximum percentage CPU consumption by each PID
DAEMON_INTERVAL=3 # Daemon check interval in seconds
BLACK_PROCESSES_LIST="dropbox" # Limit only processes defined in this variable. If variable is empty (default) all violating processes are limited.
WHITE_PROCESSES_LIST= # Limit all processes except processes defined in this variable. If variable is empty (default) all violating processes are limited.
#PID Variables
NEW_PIDS_COMMAND=
NEW_PIDS=
LIMITED_PIDS=
QUEUE_PIDS=
# Check if both of the variables BLACK_PROCESSES_LIST or WHITE_PROCESSES_LIST are defined.
if [[ -n "$BLACK_PROCESSES_LIST" && -n "$WHITE_PROCESSES_LIST" ]]
then
# If both variables are defined then error is produced.
echo "At least one or both of the variables BLACK_PROCESSES_LIST or WHITE_PROCESSES_LIST must be empty."
exit 1
# If Black_Processes_List is defined
elif [[ -n "$BLACK_PROCESSES_LIST" ]]
then
# Set NEW_PIDS_COMMAND variable to below command
NEW_PIDS_COMMAND="top -b -n1 -c | grep -E '$BLACK_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
# If White_Processes_List is defined
elif [[ -n "$WHITE_PROCESSES_LIST" ]]
then
# Set NEW_PIDS_COMMAND variable to below command
NEW_PIDS_COMMAND="top -b -n1 -c | gawk 'NR>6' | grep -E -v '$WHITE_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
else
NEW_PIDS_COMMAND="top -b -n1 -c | gawk 'NR>6 && \$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
fi
# Search and limit violating PIDs
while sleep $DAEMON_INTERVAL
do
NEW_PIDS=$(eval "$NEW_PIDS_COMMAND") # Violating PIDs
LIMITED_PIDS=$(ps -eo args | gawk '$1=="cpulimit" {print $3}') # Already limited PIDs
QUEUE_PIDS=$(comm -23 <(echo "$NEW_PIDS" | sort -u) <(echo "$LIMITED_PIDS" | sort -u) | grep -v '^$') # PIDs in queue
for i in $QUEUE_PIDS
do
cpulimit -p $i -l $CPU_LIMIT -z & # Limit new violating processes
done
done
我只想将特定的应用程序列入黑名单,因此我将其改为:
#!/bin/bash
# CPU limit of a process
#
# Variables
CPU_LIMIT=50 # Maximum percentage CPU consumption by each PID
# NOTE: If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power.
DAEMON_INTERVAL=3 # Daemon check interval in seconds
PROCESS_1="" # Processes to be limited. If variable is empty (default) all violating processes are limited.
if [ -n "$PROCESS_1" ] # Process_1 entered
then
PID_1="pidof $PROCESS_1" # Set NEW_PIDS_COMMAND variable to below command
echo "Limit Process of: $PROCESS_1"
cpulimit --pid "$PID_1" -b -l "$CPU_LIMIT" # Run cpulimit with selected paramters
else
echo "All Processes limited to: $CPU_LIMIT"
PID_1="top -b -n1 -c | gawk 'NR>6 && \$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT" # Set global CPU limit
fi
# Search and limit violating PIDs
while sleep $DAEMON_INTERVAL
do
NEW_PIDS=$(eval "$PID_1") # Violating PIDs
LIMITED_PIDS=$(ps -eo args | gawk '$1=="cpulimit" {print $3}') # Already limited PIDs
QUEUE_PIDS=$(comm -23 <(echo "$NEW_PIDS" | sort -u) <(echo "$LIMITED_PIDS" | sort -u) | grep -v '^$') # PIDs in queue
for i in $QUEUE_PIDS
do
cpulimit -p $i -l $CPU_LIMIT -z & # Limit new violating processes
done
done
这可行,但我该如何限制多个进程?目前只能限制一个进程或全部进程...谢谢
答案1
我意识到这个帖子有点老了;然而,有一个很多更简单的方法来实现您所述的目标。使用您似乎想要的流程和限制,您可以简单地创建一个 shell 脚本,例如:
#!/bin/bash
#Set this variable to the number of cores in your processor. This example is for an 8-core CPU. The reason that the number of cores is important is that you need to take it into consideration when setting cpulimit's -l option. This is explained on the cpulimit project page (see http://cpulimit.sourceforge.net/): "If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power."
NUM_CPU_CORES=8 #Change this number to reflect the number of cores in your processor.
cpulimit -e "dropbox" -l $((50 * $NUM_CPU_CORES))& #Limit "dropbox" process to 50% CPU usage.
cpulimit -e "tracker-miner-f" -l $((50 * $NUM_CPU_CORES))& #Limit "tracker-miner-f" process to 50% CPU usage.
cpulimit -e "tracker-store" -l $((50 * $NUM_CPU_CORES))& #Limit "tracker-store" process to 50% CPU usage.
cpulimit -e "tracker-extract" -l $((50 * $NUM_CPU_CORES))& #Limit "tracker-extract" process to 50% CPU usage.
cpulimit -e "chrome" -l $((40 * $NUM_CPU_CORES))& #Limit "chrome" process to 40% CPU usage.
使上述脚本可执行,并将其设置为自动运行,就像您对其他脚本所做的那样(删除其他脚本)。实际上,CPU限制它将停留在后台并监视指定的进程,一旦某个进程启动,CPU限制将接管控制并实现其指定的限制。当指定进程死亡或被终止时,CPU限制将观察它是否再次活跃,然后重复该过程。
答案2
如果您想在启动时自动启动该程序,您可以将其添加到启动应用程序中。有关更多信息,请参阅以下答案。
答案3
这是我为解决问题所做的事情:
sudo apt-get install cpulimit ; sudo apt-get install gawk ; sudo apt-get install top
然后我修改了一个脚本来适应:
#!/bin/bash
# CPU limit of a process
#
# Variables
# NOTE: If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power.
CPU_LIMIT_1=50 # Maximum percentage CPU consumption by PROCESS_1
CPU_LIMIT_2=55 # Maximum percentage CPU consumption by PROCESS_2
CPU_LIMIT_3=60 # Maximum percentage CPU consumption by PROCESS_3
CPU_LIMIT_4=40 # Maximum percentage CPU consumption by PROCESS_4
PROCESS_1="dropbox" # Process 1 to be limited. If variable 1 is empty (default) all violating processes are limited.
PROCESS_2="tracker-miner-f" # Process 2 to be limited
PROCESS_3="tracker-store" # Process 3 to be limited
PROCESS_4="tracker-extract" # Process 4 to be limited
PROCESS_5="chrome" # Process 5 to be limited
gnome-terminal -x top
if [ -n "$PROCESS_1" ] || [ -n "$PROCESS_2" ] || [ -n "$PROCESS_3" ] || [ -n "$PROCESS_4" ] || [ -n "$PROCESS_5" ] ; then
while true;
do
echo "Limit the Process of: $PROCESS_1 to $CPU_LIMIT_1"
cpulimit --exe "$PROCESS_1" -b -l "$CPU_LIMIT_1"
sleep 3
echo "Limit the Process of: $PROCESS_2 to $CPU_LIMIT_1"
cpulimit --exe "$PROCESS_2" -b -l "$CPU_LIMIT_1"
sleep 3
echo "Limit the Process of: $PROCESS_3 to $CPU_LIMIT_1"
cpulimit --exe "$PROCESS_3" -b -l "$CPU_LIMIT_1"
sleep 3
echo "Limit the Process of: $PROCESS_4 to $CPU_LIMIT_1"
cpulimit --exe "$PROCESS_4" -b -l "$CPU_LIMIT_1"
sleep 3
echo "Limit the Process of: $PROCESS_5 to $CPU_LIMIT_3"
cpulimit --exe "$PROCESS_5" -b -l "$CPU_LIMIT_4"
sleep 60
done
else
echo "Process fields empty"
fi
exit 1
在我的主菜单中创建了一个名为“开始”的文件夹,并将脚本移动到那里。
我想在需要的时候运行它,因此我使用“menulibre”创建了一个启动器,并可以选择在终端中运行。
可能有更好的方法,但就目前而言,这是可行的...欢迎提供任何更好的解决方案!
谢谢
这也用于限制单个进程或全局进程:
#!/bin/bash
# CPU limit of a process of one application or set global limit
#
#
DAEMON_INTERVAL=3 # Daemon check interval in seconds
gnome-terminal -x top
read -p "Do you want to set global CPU limitations y or n : " y
if test "$y" = "y" ; then
read -p "Enter Global CPU limit :" CPU_LIMIT_ALL
echo $'\nAll Processes shall be limited to:' $CPU_LIMIT_ALL
while true
do
PID_1="top -b -n1 -c | awk 'NR>6 && \$9>CPU_LIMIT_ALL {print \$1}' CPU_LIMIT_ALL=$CPU_LIMIT_ALL" # Set global CPU limit reads TOP list
NEW_PIDS=$(eval "$PID_1") # Violating PIDs
LIMITED_PIDS=$(ps -eo args | gawk '$1=="cpulimit" {print $3}')
# Already limited PIDs
QUEUE_PIDS=$(comm -23 <(echo "$NEW_PIDS" | sort -u) <(echo "$LIMITED_PIDS" | sort -u) | grep -v '^$') # PIDs in queue
for i in $QUEUE_PIDS
do
cpulimit -p "$i" -l "$CPU_LIMIT_ALL" -z & # Limit new violating processe
done
done
elif test "$y" = "n" ; then
read -p "Enter process to be restricted or press enter :" r
read -p "Enter value of CPU limit or press enter :" l
while true
do
echo $'\nProcess Entry Found'
echo $'CPU Entry Found\n'
echo "Limit the Process of: $r to $l"
cpulimit --exe "$r" -b -l "$l" -z & # Set CPU limit for process
sleep 60
done
else
echo "No input found"
exit 1
fi
另存为 .sh 然后使可执行文件并运行教程
如果需要,您也可以以 root 身份运行脚本来限制所有系统进程。
sudo /路径/到/脚本/