I've got an installer/updater script that's meant for IRIX/Linux/macOS/FreeBSD and I would like to extend its compatibility to Solaris.
I've already fixed a few parts that weren't POSIX compliant, except for the crontab
which is generated like this:
printf '%s\n' MAILTO=me@xyz.org '*/15 * * * * /path/cmd' | crontab -
# crontab -l # (on Linux/macOS/FreeBSD)
MAILTO=me@xyz.org
*/15 * * * * /path/cmd
note: /path/cmd
is quiet unless it detects a problem
The code fails on Solaris for three reasons:
MAILTO=
throws a syntax error*/15
throws a syntax errorcrontab -
tries to open the file named-
I fixed #2 and #3 with:
printf '%s\n' '0,15,30,45 * * * * /path/cmd' | crontab
# crontab -l
0,15,30,45 * * * * /path/cmd
Now I don't know how to convert the MAILTO=
part. What would be a POSIX way to forward emails from a crontab
?
Selected workaround:
Thanks to @ilkkachu and @Gilles'SO-stopbeingevil' pointers, here's how I decided to emulate crontab's MAILTO
behavior in a POSIX compliant way:
# crontab -l
0,15,30,45 * * * * out=$(/path/cmd 2>&1); [ -n "$out" ] && printf \%s\\n "$out" | mailx -s "Cron <$LOGNAME@$(uname -n)>" me@xyz.org
But, there's a potential problem with this solution: if printf
is not a shell builtin and the output is too big then it will fail with an Argument list too long
or the likes.