4

Does anyone know if it's possible to execute and renice a process in one command, i.e. without having to look up the command in the list of processes using the ps command and then renice that particular pid.

  • 4
    Why don't you use nice when you launch the process, instead of renice afterwards? – glenn jackman Sep 8 '11 at 2:32
  • @Glenn-jackman your post it is practically the answer. – Torian Sep 8 '11 at 4:22
1

As I mentioned, @glenn-jackman gave you the answer. But just to elaborate a bit more, if you wish to give higher priority to the command but do not intend to run it as root, you could use a function (and sudo):

nice_cmd() {
  PRIORITY=$1 ; shift
  CMD=$1 ; shift
  ${CMD} $@ & cmdpid=$!
  sudo renice -n ${PRIORITY} -p ${cmdpid}
}

Then execute it as (this could ask for your user password, depending on how is sudo configured)

$ nice_cmd -5 vim somefile
$ fg

And from a top on another terminal, you can double check the nice value.

  • 1
    You always want to enclose $@ in double quotes. You don't need to shift the command: PRI=$1; shift; "$@" & pid=$1; ... – glenn jackman Sep 8 '11 at 5:01
  • Agreed, but it is just a good measure to be more specific on how it works. – Torian Sep 8 '11 at 15:35
  • Just interested in the two downvotes, any further reason why ? – Torian Sep 9 '11 at 19:52
  • because the nice command already does that. This being the accepted answer is just confusing. – hasen Sep 11 '17 at 9:06
4

Just use nice (instead of renice). For example:

nice -n 10 command

This will run command with a low priority.

-1

Linux process scheduling using nice and renice commands with examples can also be found at http://www.vmexplore.com/tuning-process-scheduling/

  • It is better to provide action information here, and use the link as a reference for further details. That way your answer does not lose all of it value when the link becomes invalid. – Anthon Apr 6 '14 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.