有没有办法自动启动CPULIMIT以减少过多的CPU消耗

pst*_*07x 4 bash scripts cpu-load overheating 14.04

tracker-miner-f、tracker-store、tracker-extract 和 dropbox 进程正在耗尽我的 CPU,它们使其运行速度超过 100%,导致我的笔记本电脑过热。

我已经安装了 CPULIMIT,它可以很好地限制这些进程,但是,我无法让它自动启动。

我已遵循操作,但它不起作用,守护进程启动,但我认为创建脚本时存在错误,并且它实际上并不限制任何进程。

有没有一种方法可以减少 cpu 使用率,而不必每次启动新会话时都通过终端手动执行此操作?

乌班图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"
Run Code Online (Sandbox Code Playgroud)

它是否正确?

#!/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
Run Code Online (Sandbox Code Playgroud)

我只想将特定应用程序列入黑名单,因此我对此进行了更改:

#!/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
Run Code Online (Sandbox Code Playgroud)

这可行,但如何限制多个进程?目前它要么是一个,要么全部......谢谢

小智 5

我意识到这个线程有点旧了;但是,有一种简单的方法可以实现您既定的目标。使用您似乎想要的进程和限制,您可以简单地创建一个 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.
Run Code Online (Sandbox Code Playgroud)

使上述脚本可执行并将其设置为自动运行,就像处理其他脚本一样(删除其他脚本)。实际上,cpulimit将停留在后台并监视指定的进程,一旦启动进程,cpulimit将控制并实现其指定的限制。当指定的进程死亡或被终止时,cpulimit将监视它再次变得活动,并且该进程会重复。

  • 我建议第一行是“NUM_CPU_CORES=$(nproc --all)” (4认同)