opsengine / cpulimit

CPU usage limiter for Linux
Other
1.7k stars 270 forks source link

feature request: Add foreground mode #62

Open gavv opened 8 years ago

gavv commented 8 years ago

Hi,

It would be nice to have an option to run command in foreground, e.g.:

$ cpulimit -f -l10 -- ./foo bar
phil294 commented 6 years ago

this should even be the default. the user can issue background jobs using & him/herself.

Edit: I dont know what I meant by that, foreground mode is already the default as mentioned by @joetwiddle below ??

jonathancross commented 5 years ago

I just use a bash function to do this:

function cpulimit () {
  PID=$1
  REST_TIME=${2}
  REST_TIME=${REST_TIME:=1.5}
  RUN_TIME=${3}
  RUN_TIME=${RUN_TIME:=1}
  if [ "$PID" ]; then
    echo "Starting: $(date) pid=$PID rest=${REST_TIME} run=${RUN_TIME}."
    while true; do
      kill -SIGSTOP ${PID}
      sleep ${REST_TIME}
      kill -SIGCONT ${PID}
      sleep ${RUN_TIME}
      echo -n .
    done
    echo -e "Done: $(date)"
    echo "  • You can use this to continue a stopped process: kill -SIGCONT ${PID}"
  else
    echo "ERROR: You must supply pid."
    echo "USAGE:   cpulimit pid <rest-sec> <run-sec>"
    echo "EXAMPLE: cpulimit 123456 1.5 1"
  fi
}

It will output . every cycle. Just type ^c to kill the process.

Note: only tested recently on MacOS, but I see no reason it should not work on Linux.

joeytwiddle commented 3 years ago

@jonathancross That shellscript is pretty neat! Although I don't think it quite addresses the issue.

I think what @gavv wants is to specify the command to execute on the command-line, so they don't have to do any mangement of PIDs.

This would work really well with other tools, including trickle:

nice -n 15 ionice -c 3 trickle -d 20 -u 20 cpulimit -l 10 ./foo bar

Wait a minute. This is supported! Since 688fc589c3cfa0493877e84f1c5cdbacf0ef11ae. See the last line below:

Usage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      ...
   TARGET must be exactly one of these:
      -p, --pid=N            pid of the process (implies -z)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

I believe this issue can be closed.