Tell me more ×
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

2 Answers

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

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

nice -n 10 command

This will run command with a low priority.

share|improve this answer

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.