Consider the following case
nice -10 mysqldump -u crontab -p'readonly' --databases "$1" | gzip > $hour_folder"/"$1"_h"$hour".sql.gz"
I aask you if nice
is applied only to mysqldumnp
or is applied to gzip
, too?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityNo, nice
(as opposed to the !
or time
shell keywords for instance¹) is a standalone command, so it could not apply to other commands that are piped to it or that it is piped to.
The change of niceness is only applied to the process that nice
is executed in that will later execute the command passed as arguments (and all the processes that that command may itself spawn as niceness is inherited upon fork).
If you want to increase the niceness of both commands, you'd need two separate calls to nice
:
nice cmd1 | nice cmd2
Or have nice
start a shell that starts the pipeline:
nice sh -c 'cmd1 | cmd2'
Or you could increase the niceness of the current shell or subshell process with renice
prior to starting the pipeline (beware some renice
implementations set the niceness while others increase it by the specified amount).
With zsh
:
zmodload zsh/system
(renice 10 $sysparam[pid]; cmd1 | cmd2)
With bash
:
(renice 10 "$BASHPID"; cmd1 | cmd2)
In zsh
, commands started asynchronously have their niceness increased by 5 (unless the bgnice
option is disabled), so you could also do:
(cmd1 | cmd2 &) &
To increase the niceness of both commands by 10.
$ ps -o nice,args | cat
NI COMMAND
0 /bin/zsh
0 ps -o nice,args
0 cat
$ nice ps -o nice,args | cat
NI COMMAND
0 /bin/zsh
10 ps -o nice,args
0 cat
$ nice ps -o nice,args | nice cat
NI COMMAND
0 /bin/zsh
10 ps -o nice,args
10 cat
$ ps -o nice,args | cat &
NI COMMAND
0 /bin/zsh
5 ps -o nice,args
5 cat
$ (ps -o nice,args | cat &) &
NI COMMAND
0 /bin/zsh
10 ps -o nice,args
10 cat
Also beware that in nice mysqldump...
, a lot of the CPU time used by that operation will be spent by the mysqld
process handling the requests by mysqldump
, and nice
only sets the niceness of the process that is executing mysqldump
, that has no effect on the niceness of the MySQL server processes themselves.
!
and time
in bash
and some other Korn-like shells are keywords in the shell language, not commands. They apply to the following pipeline. Beware though that if there's a -p
word following time
or if time
is quoted in any way, then the time
standalone command is run instead and therefore only applies to the command passed as its arguments.
nice bash -c 'cmd1 | cmd2'
sh
instead of bash
)
Aug 27, 2020 at 8:37