0

I've been trying to get all cron job outputs to a file (not email). The alias is set in /etc/aliases.

logthecron: "|cronlog.sh"

And in crontab MAILTO=logthecron. The cronlog.sh file writes to output to some file:

#!/bin/sh
$@ 2>&1 | sed -e "s/\(.*\)/[`date`] \1/" >> /tmp/a

I am using sendmail. Sendmail uses smrsh, a restricted shell utility that provides the ability to specify, through the /etc/smrsh directory, an explicit list of executable programs available to Sendmail. So I symlinked cronlog.sh and sendmail to that directory. Something like...

ln -s /root/cron/cronlog.sh /etc/smrsh/

And still keep getting this error.

May 10 09:33:11 sandbox01 smrsh: uid 8: attempt to use "cronlog.sh"
May 10 09:33:11 sandbox01 sendmail[23870]: x4ADXB5Y023868: to="|cronlog.sh", ctladdr=<logthecron@[hostname]> (8/0), delay=00:00:00, xdelay=00:00:00, mailer=prog, pri=30787, dsn=5.0.0, stat=Service unavailable
May 10 09:33:11 sandbox01 sendmail[23870]: x4ADXB5Y023868: x4ADXB5Y023870: DSN: Service unavailable

Note: I am using CentOS v7, the file is executable, email works without issues, tried the entire directory path in the alias and I do not want to write individual cronjob outputs but write all the output of cron jobs to some file.

Reference:

  1. smrsh: http://www.faqs.org/docs/securing/chap22sec182.html

  2. Logging ALL stderr output of crontab to file

  • 1
    As you can see from your log, cron tries to send an email to |cronlog.sh. The value of MAILTO will always be taken as an email address. – Kusalananda May 10 at 13:56
  • I understand that but thats exactly what I'm trying to avoid. I want to send the cronjob output to the cronlog.sh file. Reference: unix.stackexchange.com/questions/29759/… – Div May 10 at 13:57
  • /root is usually only accessible to root, so better to put a copy of the script in /etc/smrsh. – wurtel May 10 at 14:00
  • @wurtel - Did that, the error does not change. – Div May 10 at 14:02
  • I assume the script has execute permissions for the UID running sendmail? – wurtel May 10 at 14:07
0

Instead of trying to use MAILTO (which will always be interpreted as an email address), use SHELL.

Set SHELL to the path of a small executable shell script that runs the given command with output directed to a file:

#!/bin/sh

now=$(date)
/bin/sh "$@" 2>&1 | awk -v now="$now" '{ printf("[%s]\t%s\n", now, $0) }' >/tmp/cronjob.log

The "$@" here will be expanded to -c followed by the job specification from the crontab file. It's important to write "$@" with the double quotes.

In the crontab, use

SHELL=/path/to/cronrun

# rest of crontab below...

(assuming /path/to/cronrun is the correct path to that short script)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.