Does nice work on any shell script, or does it work on basic Linux commands only?

For example, if I have a custom shell script that contains a sort function, and I call

nice myscript.sh

would the nice scheduling priority apply to sort in the script? Would nice still work if myscript.sh is running in the background (using & or nohup)?

migrated from stackoverflow.com Jul 20 '12 at 11:37

This question came from our site for professional and enthusiast programmers.


Yes, the nice command can be used with scripts. Actually, assuming you are using bash, the nice command will be applied to bash and all its possible child processes. The nice priority is not altered when a process is running on the background.

You can just test it yourself and see that nice indeed affects the priority of a bash script. Just execute any script you want, and then use ps to see the priorities:

ps -eo pid,pri,cmd

Nice applies to the process that is started when running the command and any child processes. So the answer to your question is yes, it does work on any commands/scripts, even if they are put into the background.

Your Answer

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