SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a sh script that does a bunch of processing within a list block and the output of that block is piped to mail, so I don't want extraneous output from the block.
For diagnostics and debugging, at a few steps within the block I echo some info to stderr (to avoid mail) with `echo $VAR >> /dev/stderr`. This works fine when the script is run from a terminal. When it is run from cron, I get "cannot create /dev/stderr: Permission denied".
I tested cron using `id` and the jobs are being run as my user and my groups.
How can cron output to stderr so that that particular output is mailed as part of cron's normal mail output?
Yes, I know about redirection. The problem is that I want to keep stdout and stderr separate streams because stdout is piped to mailx. I also want stderr's output so that cron's mail will mail it to me. The problem is that the script can write to stderr when run from a terminal, but gets a permission error when run from cron.
Yes, I know about redirection. The problem is that I want to keep stdout and stderr separate streams because stdout is piped to mailx. I also want stderr's output so that cron's mail will mail it to me. The problem is that the script can write to stderr when run from a terminal, but gets a permission error when run from cron.
So "echo $VAR 1>&2" doesn't work? Is that what you're telling me?
That does work. Sorry for dismissing it so quickly, I was thinking about the block rather than just that one command.
I did some exploring and the "definitive" file descriptors are 0, 1 & 2 rather than stdin|out|err, which may be counter-intuitive. Here is the output from a cron job of:
Code:
05 12 * * * ls -l /dev/std* /dev/fd/* /proc/self/fd/* /var/spool/cron/ /var/spool/cron/cron.Va31y9/
gives
ls: cannot access /dev/fd/3: No such file or directory
ls: cannot access /proc/self/fd/3: No such file or directory
lrwx------ 1 nick users 64 May 12 12:06 /dev/fd/0 -> /dev/null
l-wx------ 1 nick users 64 May 12 12:06 /dev/fd/1 -> /var/spool/cron/cron.Va31y9/cron.nick.8762
l-wx------ 1 nick users 64 May 12 12:06 /dev/fd/2 -> /var/spool/cron/cron.Va31y9/cron.nick.8762
lrwxrwxrwx 1 root root 15 Apr 19 20:05 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Apr 19 20:05 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Apr 19 20:05 /dev/stdout -> /proc/self/fd/1
lrwx------ 1 nick users 64 May 12 12:06 /proc/self/fd/0 -> /dev/null
l-wx------ 1 nick users 64 May 12 12:06 /proc/self/fd/1 -> /var/spool/cron/cron.Va31y9/cron.nick.8762
l-wx------ 1 nick users 64 May 12 12:06 /proc/self/fd/2 -> /var/spool/cron/cron.Va31y9/cron.nick.8762
ls: cannot open directory /var/spool/cron/: Permission denied
/var/spool/cron/cron.Va31y9/:
total 8
-rw------- 1 root root 1054 May 12 12:06 cron.nick.8762
and cron.Va31y9 is only writable by root, which possibly explains why writing to /dev/stderr won't work. I think. I still don't really understand why writing to fd2 does work though.
Well, yeah, that's the way C runtime has done it from the beginning. It's really the case that for any process, file descriptor 0 is called stdin, file descriptor 1 is called stdout, and file descriptor 2 is called stderr.
Writing to fd 2 works for the same reason that writing to fd 1 works. Magic. :-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.