Page 5 of 13 FirstFirst ... 34567 ... LastLast
Results 41 to 50 of 122

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

  1. #41
    Join Date
    Mar 2007
    Beans
    623

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

    kut77less,
    sorry for late response, I have been very busy.

    It looks "grep" command works differently if "." character is written in variable, don't know why this kind of behaviour. Work-around solution is to to replace the following string in row 19:
    Code:
    grep -v '$WHITE_PROCESSES_LIST'
    with code
    Code:
    grep -v "$WHITE_PROCESSES_LIST"
    So instead of single quotes use double quotes.

    This should work.

    If you don't like above solution, then just use "npviewer" as WHITE_PROCESS_LIST variable, so without "." character.

    Please write back if you solved the problem.
    Regards

  2. #42
    Join Date
    Jul 2009
    Beans
    2

    Re: Doesn't work when grep not linked to egrep

    Hi, thank you very much for these instructions! However for me it didn't work unless I replaced "grep 'black/white list' with "grep -E 'black/white list'" in /root/cpulimit_daemon.sh:

    Code:
    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 -v -E '$WHITE_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT && \$1 != \"PID\" {print \$1}' CPU_LIMIT=$CPU_LIMIT"
    else
       NEW_PIDS_COMMAND="top -b -n1 -c | gawk 'NR>6 && \$9>CPU_LIMIT  && \$1 != \"PID\" {print \$1}' CPU_LIMIT=$CPU_LIMIT"
    fi
    I suppose that on your system grep is actually linked to egrep, however on my system (Ubuntu 9.04) it's "GNU grep 2.5.3", which doesn't interpret its input as a regular expression by default and we must enforce it by providing the -E option.

    Could you please reflect this possible issue somehow in the HOWTO to save other people from the pain to find it out? Thank you!

  3. #43
    Join Date
    Mar 2007
    Beans
    623

    Re: Doesn't work when grep not linked to egrep

    Quote Originally Posted by malyvelky View Post
    I suppose that on your system grep is actually linked to egrep, however on my system (Ubuntu 9.04) it's "GNU grep 2.5.3", which doesn't interpret its input as a regular expression by default and we must enforce it by providing the -E option.
    Hi,
    I have checked with command "which grep" and it returned /bin/grep and "grep --help" returned "GNU grep 2.5.4". Now I use Ubuntu 9.10, but when this script was last updated on first post of this thread I used Ubuntu 9.04 and it was working fine.

    Can you please write what is your string for white or black list that was not excepted by grep command. I need this to check in deep why the exception appeared. And if needed I will update the first post, but I need more info.
    Regards
    Last edited by abcuser; November 26th, 2009 at 08:30 PM.

  4. #44
    Join Date
    Mar 2009
    Beans
    58

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

    Quote Originally Posted by abcuser View Post
    kut77less,
    sorry for late response, I have been very busy.

    It looks "grep" command works differently if "." character is written in variable, don't know why this kind of behaviour. Work-around solution is to to replace the following string in row 19:
    Code:
    grep -v '$WHITE_PROCESSES_LIST'
    with code
    Code:
    grep -v "$WHITE_PROCESSES_LIST"
    So instead of single quotes use double quotes.

    This should work.

    If you don't like above solution, then just use "npviewer" as WHITE_PROCESS_LIST variable, so without "." character.

    Please write back if you solved the problem.
    Regards
    Unfortunately this has not solved my problem.

    Thanks for your efforts.

    Any other ideas?

  5. #45
    Join Date
    Mar 2007
    Beans
    623

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

    Hi,
    what did you test?
    1. double quotes in grep or
    2. just use "npviewer" in WHITE_PROCESS_LIST option?

    I have checked and both of this solution should work.
    What is your Ubuntu version? What does command "which grep" outputs? What does command "grep --version" outputs?
    Regards

  6. #46
    Join Date
    Mar 2009
    Beans
    58

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

    Quote Originally Posted by abcuser View Post
    Hi,
    what did you test?
    1. double quotes in grep or
    2. just use "npviewer" in WHITE_PROCESS_LIST option?

    I have checked and both of this solution should work.
    What is your Ubuntu version? What does command "which grep" outputs? What does command "grep --version" outputs?
    Regards
    Thanks for your reply. This is what I have


    #!/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= # Limit only processes defined in this variable. If variable is empty (default) all violating processes are limited.
    WHITE_PROCESSES_LIST=npviewer # 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 '$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 -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



    The commands you told me to input said this

    /bin/grep
    and

    GNU grep 2.5.4
    I use Ubuntu Karmic Koala

    Thanks for all your help

  7. #47
    Join Date
    Mar 2007
    Beans
    623

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

    kut77less, you are using the same system as I, so it is strange you get different result.
    Can you please run you program and check what you get for output of the following command:
    Code:
    top -b -n1 -c | gawk 'NR>6' | grep -v "npviewer"

  8. #48
    Join Date
    Nov 2007
    Beans
    2

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

    I have Hardy (8.04) and have installed the daemon. Good work. (I also added sysvconfig per your instructions).

    The only thing that doesn't work quite right on my system is as follows:

    When issuing the sudo service cpulimit restart command, I receive the following output:

    Code:
    doxola@athena:~$ sudo service cpulimit restart
    Usage:
      kill pid ...              Send SIGTERM to every process listed.
      kill signal pid ...       Send a signal to every process listed.
      kill -s signal pid ...    Send a signal to every process listed.
      kill -l                   List all signal names.
      kill -L                   List all signal names in a nice table.
      kill -l signal            Convert between signal numbers and names.
    doxola@athena:~$
    Not sure why this is, as the other commands work as they should, e.g.:

    Code:
    doxola@athena:~$ sudo service cpulimit status
     * cpulimit daemon is running
    doxola@athena:~$
    This is a minor issue by all accounts, as the daemon runs as its supposed to otherwise. Instead of performing the restart command, I just stop and start the daemon using the other available commands.

    Again, good work!


  9. #49
    Join Date
    Mar 2007
    Beans
    623

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

    Quote Originally Posted by doxola View Post
    When issuing the sudo service cpulimit restart command, I receive the following output:

    Code:
    doxola@athena:~$ sudo service cpulimit restart
    Usage:
      kill pid ...              Send SIGTERM to every process listed.
      kill signal pid ...       Send a signal to every process listed.
      kill -s signal pid ...    Send a signal to every process listed.
      kill -l                   List all signal names.
      kill -L                   List all signal names in a nice table.
      kill -l signal            Convert between signal numbers and names.
    doxola@athena:~$
    Not sure why this is, as the other commands work as they should,
    The script tries to find cpu_daemon and execute kill command. In your sample above kill command does not get any argument, process to kill, so kill syntax help is displayed. This is actually not such a big problem, it just can't stop the daemon that is already stopped. After this command is run then start command is executed which should not produce any error.

    Just to make sure if this my assumption is correct I am wondering how did you execute commands? Just wondering if you have done in the following sequence:
    Code:
    sudo service cpulimit stop
    sudo service cpulimit restart
    sudo service cpulimit status
    if so, then first command (stop) killed the cpu_daemon and the second (restart) tried to kill the daemon again, but daemon was not running because it was already killed by stop command execute before this restart command. In this case this "kill syntax help error" is displayed. Then restart command also executes start, so status command correctly showed that daemon is running. So daemon couldn't be stopped because it was already stopped and was just started. Actually this is what you want.

    To make it sure, please test in the following way:
    Code:
    sudo service cpulimit start
    sudo service cpulimit restart
    and then see if restart command is returning any error, it should not return the error.

    If you would like to test if restart is working correctly. Then test:
    1. open /etc/init.d/cpulimit file with text editor and increase sleep time from 5 to 60 seconds, so change code:
    Code:
    restart)
    $0 stop
    sleep 5
    $0 start
    to:
    Code:
    restart)
    $0 stop
    sleep 60
    $0 start
    this will allow you to have one minute to test what is happening (after this test is over you can set it back to 5 seconds).
    2. Open terminal and execute:
    Code:
    sudo service cpulimit stop
    to stop the daemon.
    3. Execute:
    Code:
    sudo service cpulimit restart
    to restart daemon. This should print "kill syntax help error", which is just a info, not something to be doing wrong.
    4. Open another terminal and check if daemon is running (don't forget you have only 60 seconds to do this, if you are slow writer, then increase to more then 60 seconds):
    Code:
    sudo service cpulimit status
    it should report that daemon is not running, which is ok, because in step 2 you have stopped the daemon.
    5. After 60 seconds start command will be executed because restart executes stop and after 60 seconds executes start.
    6. Check again if cpulimit is running:
    Code:
    sudo service cpulimit status
    and it should report that it is running.

    If this is the case, then the only problem is that when restarting daemon, daemon can't be stopped because it was already stopped by "stop" command and so can't be stopped one more time, so just warning is put out "kill syntax help error", nothing to be worried about, daemon it is working fine.

    If above is not working then try the following:
    1. Change the code:
    Code:
    restart)
    $0 stop
    sleep 5
    $0 start
    to:
    Code:
    restart)
    ps -eo pid,args | gawk '$3=="/root/cpulimit_daemon.sh"  {print $1}' | xargs kill -9   # kill cpulimit daemon
    ps -eo pid,args | gawk '$2=="cpulimit" {print $1}' | xargs kill -9       # release cpulimited process to normal priority
    sleep 5
    nohup /root/cpulimit_daemon.sh &
    So instead of stop and start aliases just use full commands from stop and restart.

    Please report if this is work-around for you system.

    I am at my holiday vacations and will not be using Ubuntu 8.04 until early stage of January 2010, to test this out. I would be very happy if you could test this out and report if daemon is executing as I have described in this post.

    When I will have some time I will check if this error message can be avoided. So to check first if daemon is not running and to not try to kill the process that is not running. This can probably be done with some if checking but as I wrote above, I have to find some time to fix this. But it would help me much if you could test above scenario and write how restart is behaving.

    Thank you very much to test this daemon and report problem you have.
    Last edited by abcuser; December 29th, 2009 at 06:09 PM.

  10. #50
    Join Date
    Mar 2007
    Beans
    623

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

    doxola,
    I have tested the code. The problem is in "sudo service cpulimit stop". If this command is executed two times in a role then second time the error is displayed that there is no process to kill (which is correct). This is actually not a big problem, just some confusing info users are confused about.

    Because "sudo service cpulimit restart" calls stop command (and then start command) the problem also appears in "restart" command, just like you have reported. It is also just annoying info, but script is working correctly.

    Today I have added some if logic to check if process is stopped and if it is the script will not try to stop the process (and so reporting error), but instead it will report info, that process can't be stopped, because it is already stopped.

    I have also added some additional checks in "start" command, to prevent multiple starts of the script if "sudo service cpulimit start" is executed twice or multiple times. Now scripts checks if the process is started and if it is, then it reports info, that process can't be started, because it is already running.

    I have also added additional checks for start/stop/restart right after command is executed, to check if start/stop/restart was successfully executed or not and then report info of success or failure.

    Doxola, thanks a lot for reporting this problem. I hope I have fixed all the problems related to this reported issue. I have changed script on the first post of this thread.
    Regards
    Last edited by abcuser; January 4th, 2010 at 05:36 PM.

Page 5 of 13 FirstFirst ... 34567 ... 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
  •