4

tracker-miner-f, tracker-store, tracker-extract and dropbox processes are killing my CPU, they make it run at over 100%, overheating my laptop.

I have installed CPULIMIT and it works great restricting these processes, however, I cannot get it to autostart.

I have followed this but it does not work, the daemon starts but I think there is an error in the creation of the script and it does not actually limit any processes.

Is there a way to reduce cpu usage without having to manually do it through terminal every time I start a new session?

Ubuntu 14.04

A script I have found below, only partly runs and does not detect the PID correctly.

If someone has better knowledge than me, could you check the line:

NEW_PIDS_COMMAND="top -b -n1 -c | grep -E '$BLACK_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"

Is this correct?

#!/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

I only want to black list specific applications so I have altered to this:

#!/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

This works, but how do I limit multiple processes? At the moment it is either one or all ... Thanks

7
  • "the daemon starts but I think there is an error in the creation of the script and it does not actually limit any processes." why no t focus on fixing this? see your log files in /var/log/ and track down the error.
    – Rinzwind
    Jun 8, 2015 at 14:20
  • yeah easier said than done my friend... I have been trying to work out why the the line : BLACK_PROCESSES_LIST= # Limit only processes defined in this variable. If variable is empty (default) all violating processes are limited....... Does not actually do anything, yet running the 'cpulimit' directly in terminal does... I am trying to work out how to create a script to run the terminal commands at boot... All new to me :)
    – pst007x
    Jun 9, 2015 at 7:31
  • What is your CPU governor set to? See cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor.
    – terdon
    Jun 9, 2015 at 11:37
  • The script works as expected on my system. Is there a dropbox process running and taking up >40% CPU on your system? Please edit your question and include the output of ps -aux | grep dropbox showing the offending process.
    – terdon
    Jun 9, 2015 at 13:11
  • My CPU Governor is set to POWERSAVE
    – pst007x
    Jun 9, 2015 at 14:11

3 Answers 3

5

I realize this thread is a bit old; however, there is a much easier way to accomplish your stated goal. Using the processes and limits you seem to desire, you can simply create a shell script such as:

#!/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.

Make the above script executable and set it to auto-run as you did with your other scripts (get rid of the other scripts). In effect, cpulimit will stay in the background and watch for the specified processes and, as soon as one is started, cpulimit will take control and effect its specified limit. When a specified process dies or is killed, cpulimit will watch for it to become active again, and the process repeats.

1
  • 4
    I suggest the first line to be NUM_CPU_CORES=$(nproc --all)
    – Sohail Si
    Sep 4, 2018 at 21:18
1

If you'd like to start the program automatically on startup, you can add it to your startup applications. See the following answer for more information.

How do I start applications automatically on login?

1

This is what I have done to resolve my issue:

sudo apt-get install cpulimit ; sudo apt-get install gawk ; sudo apt-get install top

Then I altered a script to suit:

    #!/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

Created a folder in my home menu called it start and moved the script there.

I want to run it when I wanted so I created a launcher using 'menulibre' with the option to run in terminal.

There may be a better way, but for now this works... any better solutions welcome!

Thanks

Also this for restricting an individual process or global:

#!/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

Save as a .sh then make executable and run tutorial

If you need to you can also run the scripts as root to restrict all system processes too.

sudo /path/to/script/

You must log in to answer this question.

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