Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
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
add comment

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
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
add comment

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

nice -n 10 command

This will run command with a low priority.

share|improve this answer
add comment

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

share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

    
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 at 17:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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