Page 9 of 13 FirstFirst ... 7891011 ... LastLast
Results 81 to 90 of 122

Thread: HOWTO: Set maximum CPU consumption in percentage by any process

  1. #81
    Join Date
    Oct 2011
    Beans
    13

    Re: HOWTO: Set maximum CPU consumption in percentage by any process

    I'm 99% sure I stopped it and verified the processes, but that 1% uncertainty has a nasty tendency to assert its self. I've stopped/started and verified no cpulimit/cpulimit_daemon.sh processes.

    I'll post again after I look into it further in the next few days.

    Thanks!
    Dave

  2. #82
    Join Date
    Oct 2011
    Beans
    13

    Re: HOWTO: Set maximum CPU consumption in percentage by any process

    No luck, restarted and verified all cpulimit processes were gone, today I see it limiting 2 processes in the whitelist. Odd, I'll add some debug statements and will report back.

  3. #83
    Join Date
    Mar 2007
    Beans
    680

    Re: HOWTO: Set maximum CPU consumption in percentage by any process

    David,
    why do you execute program like this:
    /bin/bash /opt/openvpn/bin/statusreader
    /bin/bash /opt/squid/bin/log_helper

    Are this bash scripts? If you open this two files what is the first statement in the file? Is it #!/bin/bash if yes, then you don't need to execute it like /bin/bash some_program, just execute it /opt/openvpn/bin/statusreader
    If #!/bin/bash is not added, can you manually add this line at firt line position and execute the program with /opt/openvpn/bin/statusreader

    I am not 100% sure, but space in the process name can cause some problems. I haven't test this, but it is possible that "openvpn" is searched in "/bin/bash" column, where nothing is found out.
    Regards

  4. #84
    Join Date
    Oct 2011
    Beans
    103

    Re: HOWTO: Set maximum CPU consumption in percentage by any process

    hi!

    i have an i7 2600k which has 4 cores (8 with ht)

    i was trying to limit the folding@home process to about 50% of the total cpu, because on full load the cpu gets really hot (been saving to buy a good heat sink)

    so, the problem is that when i try to set --limit=400 (cause the max is 800), cpulimit wont actually limit, not even if i set it to 100 or any other number above 100

    but, if i set --limit=99 cpu overall usage goes to 14% on conky, and 99% on top

    anyway i was hoping some here would help me to get cpu limit to actually limit a process to 50% of my cpu (400% on top)

  5. #85
    Join Date
    Aug 2012
    Beans
    2

    Set maximum CPU consumption in Fedora

    Hi,
    Thank you for the great job,
    I'm trying to use your scrip on Fedora with a few changes.
    In /etc/init.d/cpulimit scrip I added
    Code:
    #!/bin/sh
    #
    # Script to start CPU limit daemon
    #
    # chkconfig: 2345 99 99
    #
    # description: Starts cpulimit processes for all
    And instead of
    Code:
    update-rc.d cpulimit defaults
    I did
    Code:
    chkconfig --add cpulimit
    All looks good
    Code:
    service cpulimit status
    *cpulimit daemon is running
    But in services shows that "This unit has finished."
    Code:
    system-config-services
    My question is, Is this unit should keep running? If yes, How can I change it to the unit is running?

    Thanks in advance.
    Cheers
    Danny

  6. #86
    Join Date
    Mar 2007
    Beans
    680

    Re: Set maximum CPU consumption in Fedora

    @edanny191, maybe "system-config-services" requires something more from daemon to do, to be recognized as "running service". Program "system-config-services" is Fedora specific (not available on Ubuntu - or if it is, it's not there by default), so I am probably not be able to help you out with this problem, because I am not a Fedora user. You should probably ask on Fedora forum, something like:
    Bash script is executed in while-do infinite loop using "chkconfig --add" command and bash process resists in memory and working fine, but "system-config-services" reports "This unit has finished". Is there some additional requirement to be done by bash script to be fully recognized as 'running service'?


    On the other hand, just for explanation "service cpulimit status" executes the code I have written, with 'ps' command it just checks if process /usr/bin/cpulimit_daemon.sh is still running and if it is it outputs "*cpulimit daemon is running". Bellow is full code if it helps:

    Code:
    ps -eo pid,args | gawk '$3=="/usr/bin/cpulimit_daemon.sh"  {print}' | wc -l | gawk '{ if ($1 == 1) print " * cpulimit daemon is running"; else print " * cpulimit daemon is not running" }'
    The simpliest way of checking if daemon is working fine is to execute some CPU intensive task and check with 'top' command if CPU consumption is omitted or not. See "6. CHECK CPU CONSUMPTION WITH OR WITHOUT CPULIMIT DAEMON" chapter in HowTo for details.

    P.S. Please report on this forum if you found out the solution.
    Regards
    Last edited by abcuser; August 19th, 2012 at 11:18 AM.

  7. #87
    Join Date
    Aug 2007
    Location
    Belgium
    Beans
    176
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Set maximum CPU consumption in Fedora

    Thanks for the script, very useful!
    I just did a few modifications for my specific use, but one of them could be useful to more people:
    Code:
    start)
    if [ $(ps -eo pid,args | gawk '$3=="/usr/bin/cpulimit_daemon.sh"  {print $1}' | wc -l) -eq 0 ]; then
        nohup /usr/bin/cpulimit_daemon.sh >/dev/null 2>&1 &
        sleep 1
        ps -eo pid,args | gawk '$3=="/usr/bin/cpulimit_daemon.sh"  {print}' | wc -l | gawk '{ if ($1 == 1) print " * cpulimit daemon started successfully"; else print " * cpulimit daemon can not be started" }'
    else
        echo " * cpulimit daemon can't be started, because it is already running"
    fi
    ;;
    This is because when issuing "sudo service start" it would tell me "daemon not started" but doing "sudo service status" right after would tell me it is actually running, sleep 1 fixes this...
    El Belgicano
    -----------------
    Laptop: 5 years old Asus M6N (ATI9600/9700 graphics, 512Mb RAM, Intel Mobile 1.66GHz, 60Gb HDD) running 10.04-Lucid Lynx pretty nicely.

  8. #88
    Join Date
    Aug 2012
    Beans
    2

    Re: Set maximum CPU consumption in Fedora

    Thank you,
    I made few changes again and the problem solved,
    Here you are:
    Code:
    [root@pc ~]# gedit /usr/local/sbin/cpulimit_daemon.sh
    Code:
    #!/bin/bash
    # ==============================================================
    # CPU limit daemon - set PID's max. percentage CPU consumptions
    # ==============================================================
    #
    ADDR=root@localhost
    #
    # Variables
    CPU_LIMIT=20       	# Maximum percentage CPU consumption by each PID
    DAEMON_INTERVAL=3  	# Daemon check interval in seconds
    BLACK_PROCESSES_LIST=   # 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.
    
    # Check if one of the variables BLACK_PROCESSES_LIST or WHITE_PROCESSES_LIST is 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
    elif [[ -n "$BLACK_PROCESSES_LIST" ]] ; then                                 # If this variable is non-empty then set NEW_PIDS_COMMAND variable to bellow command
       NEW_PIDS_COMMAND="top -b -n1 -c | grep -E '$BLACK_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
    elif [[ -n "$WHITE_PROCESSES_LIST" ]] ; then                                 # If this variable is non-empty then set NEW_PIDS_COMMAND variable to bellow 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
    Code:
    [root@pc ~]# chmod 755 /usr/local/sbin/cpulimit_daemon.sh
    Code:
    [root@pc ~]# gedit /etc/rc.d/init.d/cpulimit
    Code:
    #!/bin/sh
    #
    # Script to start CPU limit daemon
    #
    #chkconfig:     35 99 01
    #
    # Source function library.
    . /etc/rc.d/init.d/functions
    [ -f /usr/local/sbin/cpulimit_daemon.sh ] || exit 0
    set -e
    
    case "$1" in
    start)
    if [ $(ps -eo pid,args | gawk '$3=="/usr/local/sbin/cpulimit_daemon.sh"  {print $1}' | wc -l) -eq 0 ]; then
        nohup /usr/local/sbin/cpulimit_daemon.sh >/dev/null 2>&1 &
        ps -eo pid,args | gawk '$3=="/usr/local/sbin/cpulimit_daemon.sh"  {print}' | wc -l | gawk '{ if ($1 == 1) print " * cpulimit daemon started successfully"; else print " * cpulimit daemon can not be started" }'
    else
        echo " * cpulimit daemon can't be started, because it is already running"
    fi
    ;;
    stop)
    CPULIMIT_DAEMON=$(ps -eo pid,args | gawk '$3=="/usr/local/sbin/cpulimit_daemon.sh"  {print $1}' | wc -l)
    CPULIMIT_INSTANCE=$(ps -eo pid,args | gawk '$2=="cpulimit" {print $1}' | wc -l)
    CPULIMIT_ALL=$((CPULIMIT_DAEMON + CPULIMIT_INSTANCE))
    if [ $CPULIMIT_ALL -gt 0 ]; then
        if [ $CPULIMIT_DAEMON -gt 0 ]; then
            ps -eo pid,args | gawk '$3=="/usr/local/sbin/cpulimit_daemon.sh"  {print $1}' | xargs kill -9   # kill cpulimit daemon
        fi
    
        if [ $CPULIMIT_INSTANCE -gt 0 ]; then
            ps -eo pid,args | gawk '$2=="cpulimit" {print $1}' | xargs kill -9                    # release cpulimited process to normal priority
        fi
        ps -eo pid,args | gawk '$3=="/usr/local/sbin/cpulimit_daemon.sh"  {print}' | wc -l | gawk '{ if ($1 == 1) print " * cpulimit daemon can not be stopped"; else print " * cpulimit daemon stopped successfully" }'
    else
        echo " * cpulimit daemon can't be stopped, because it is not running"
    fi
    ;;
    restart)
    $0 stop
    sleep 3
    $0 start
    ;;
    status)
    ps -eo pid,args | gawk '$3=="/usr/local/sbin/cpulimit_daemon.sh"  {print}' | wc -l | gawk '{ if ($1 == 1) print " * cpulimit daemon is running"; else print " * cpulimit daemon is not running" }'
    ;;
    esac
    exit 0
    Code:
    [root@pc ~]# chown root:root /etc/rc.d/init.d/cpulimit
    Code:
    [root@pc ~]# chmod 755 /etc/rc.d/init.d/cpulimit
    Code:
    [root@pc ~]# chkconfig --add cpulimit
    Code:
    [root@pc ~]# service cpulimit status
     * cpulimit daemon is not running
    Code:
    [root@pc ~]# service cpulimit restart
    Restarting cpulimit (via systemctl):                       [  OK  ]
    Code:
    [root@pc ~]# service cpulimit status
     * cpulimit daemon is running
    And in system-config-services shows, This unit is running.
    I'll check it for bugs and hope other help me too.

    Thanks again,
    Cheers
    Danny
    Last edited by edanny191; August 19th, 2012 at 12:14 PM.

  9. #89
    Join Date
    Oct 2012
    Beans
    4

    Re: HOWTO: Set maximum CPU consumption in percentage by any process

    Hi,

    Thank you for your work on these topic! It has helped me a ton so far.

    I am trying to limit the Plex Transcoder process so that I can run multiple video streams. I can successfully limit it manually, over and over for each process instance, without issue. When I use the scripts that you've built, it does indeed work for me, but I have to use the more generic command title "plex"
    Code:
    BLACK_PROCESSES_LIST="plex"
    The actual command title is "Plex Transcoder".

    When I use "plex" it seems to interfere with the streaming service or something causing all streams, even if it's just one, to pause or buffer about every minute or so.


    If I set the blacklist to
    Code:
    BLACK_PROCESSES_LIST="'/usr/lib/plexmediaserver/Resources/Plex Transcoder'"
    then, I receive the output
    Code:
    $sudo cpulimit_daemon.sh
    grep: Transcoder: No such file or directory
    If I use
    Code:
    BLACK_PROCESSES_LIST="`/usr/lib/plexmediaserver/Resources/Plex\ Transcoder`"
    it seems to work, but the output reads:
    Code:
    /usr/lib/plexmediaserver/Resources/Plex Transcoder: error while loading shared libraries: libass.so.4: cannot open shared library object file: No such file or directory

    If I use
    Code:
    BLACK_PROCESSES_LIST="'/usr/lib/plexmediaserver/Resources/Plex\ Transcoder'"
    or
    Code:
    BLACK_PROCESSES_LIST="Plex\ Transcoder"
    I don't receive an error, but it does not effect the instance(s) of Plex Transcoder.

    here is the output from "top -b -n1 -c"
    Code:
      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
     3421 plex      15  -5 1179m 213m 4896 R  362  5.4   7:02.50 /usr/lib/plexmediaserver/Resources/Plex Transcoder transcode http://127.0.0.1:32400/library/parts/657/file.mkv /tmp/plex-transcode-EADFE28A-3CE5-41A9-9EAE-9E04DC167EC7-331029a38c1ce49d4241869aac9baa11243701a3/media 7 4 27 1 nil 2 yes UTF-8

    Is it possible to blacklist a process with a _space_ in its name?

    Am I using the wrong variable for the blacklist?


    Thanks in advance!
    Last edited by brettgavin; October 5th, 2012 at 05:30 AM.

  10. #90
    Join Date
    Mar 2007
    Beans
    680

    Re: HOWTO: Set maximum CPU consumption in percentage by any process

    @brettgavin, I investigated this problem and it looks like if there is a space in process name then script does not work, error is produced by "grep -E" command.

    According to the grep documentation syntax for BLACK_PROCESS_LIST variable should be:
    Code:
    BLACK_PROCESSES_LIST='Plex[[:blank:]]Transcoder'
    Please report back if this solves the problem.

    P.S. Still don't understand why programs in Linux use spaces in file name - this just produces problems...
    Thanks
    Last edited by abcuser; October 5th, 2012 at 09:38 AM.

Page 9 of 13 FirstFirst ... 7891011 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •