8

Why do some Linux utility programs send output for "normal" operation to STDERR instead of STDOUT?

For example, when I run the following command (in CentOS 7.3):

/usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg 1> /dev/null ; echo $?

I see the output on the TTY even though the exit status is zero and there are no errors.

Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-514.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-514.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-1ef39414718640fe9f6ec0f6d65fdc3e
Found initrd image: /boot/initramfs-0-rescue-1ef39414718640fe9f6ec0f6d65fdc3e.img
done
0

I've noticed a handful of other programs that do this as well. Wondering if there is some good explanation.

For reference, I need to call this this line in an RPM %post script and I'd like to redirect "normal" output to /dev/null and capture only true "error" type messages. This particular command does not offer a -q quiet switch.

1 Answer 1

8

grub2-mkconfig normally writes the generated config file to stdout so you can redirect it conveniently. Status information is written to stderr since it would interfere with the redirected output. When you use the -o argument instead, the latter behavior stays the same, which it should for sake of consistency:

# grub2-mkconfig >/tmp/grub.conf.a
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-4.8.15-200.fc22.x86_64
Found initrd image: /boot/initramfs-4.8.15-200.fc22.x86_64.img
Found linux image: /boot/vmlinuz-4.4.14-200.fc22.x86_64
Found initrd image: /boot/initramfs-4.4.14-200.fc22.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-b764638bab7b90d16d0510c14a429d80
Found initrd image: /boot/initramfs-0-rescue-b764638bab7b90d16d0510c14a429d80.img
Found Fedora release 22 (Twenty Two) on /dev/sda2
done
# grub2-mkconfig -o /tmp/grub.conf.b
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-4.8.15-200.fc22.x86_64
Found initrd image: /boot/initramfs-4.8.15-200.fc22.x86_64.img
Found linux image: /boot/vmlinuz-4.4.14-200.fc22.x86_64
Found initrd image: /boot/initramfs-4.4.14-200.fc22.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-b764638bab7b90d16d0510c14a429d80
Found initrd image: /boot/initramfs-0-rescue-b764638bab7b90d16d0510c14a429d80.img
Found Fedora release 22 (Twenty Two) on /dev/sda2
done
# diff -u /tmp/grub.conf.{a,b}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .