Re: Using /dev/stderr vs. >&2
"Todd A. Jacobs" <firstname.lastname@example.org> writes:
> echo "shell: $SHELL"
> echo "Testing /dev/stderr: "
> echo foo > /dev/stderr
> echo "Testing >&2: "
> echo bar >&2
> The two should be equivalent, so why am I getting permission errors on
> the first but not the second?
While their effect should be the same, the way they work is quite
different. The first of this does an open(2) on "/dev/stderr", which
actually goes out to the filesystem looking for that file, and so is
subject to permission checks. The second just uses file descriptor 2
(if echo were not a shell builtin it would probably use dup(2)), so
doesn't hit the filesystem at all and can't fail permission checks.
Perhaps the child process created by cron called setuid(2) or one of
its variants, and no longer has permission to open its own
"/dev/stderr". Try adding to your test script:
# Check out /dev/stderr and friends
ls -nld /dev/stderr
ls -nld /proc
ls -nld /proc/self
ls -nld /proc/self/fd
ls -nld /proc/self/fd/2
ls -nldL /proc/self/fd/2
# Find out our identity
for re in r ''; do
for ug in u g G; do
echo -n "$re$ug: "