I'm working on a wrapper script to log cronjob output to journald.
I have several goals:
- must log stderr and stdout to journald with different priority levels and prefix-tags
- must also output stderr of the wrapped command to stderr of the wrapper script (to be caught by cron and emailed in an alert)
- must preserve line order throughout
So far I seem to have two problems:
- I'm not sure how to redirect stderr and stdout separately to my logging function. Everything I've tried thus far fails to redirect anything more than NULL. In my defense, redirection is not one of my strong points. (Resolved, see comments)
- My current approach
{ $_EXEC $_ARGS 2>&1 1>&7 7>&- | journaldlog error; } 7>&1 1>&2 | journaldlog info
appears to run the logging function exactly twice, once for stderr and once for stdout. This will not preserve line order. I would rather it run the function once per output line.
Should I just give up on doing this in bash and try Perl instead? I'm sure my Perl experience would come back to me... eventually.
I've tried a lot of different approaches. Most of which found online and, in particular, on stack exchange. I honestly can't remember what else I've tried... it's just a blur at this point. And the bash docs haven't been much help either.