1

I don't have MTA installed on my desktop.

Whenever there is a problem with a cronjob script, I see this in my logs:

CRON:  (CRON) info (No MTA installed, discarding output)

A script that was supposed to be run by cron generated an error, and cron wanted to send me the error per email.

But I would like to see the error in my log instead, ie being logged normally to syslog, same as the above info message.

Is it possible to tell cron to forget about MTA, and log everything, including errors, to local syslog ?

UPDATE

the solution from @roaima works well for my original problem as stated. But I have realized I need more sophisticated syntax for my cronjob, where stdout from command1 is piped to command2 and stderr (from both?) is piped to command3.

Here is a concrete example (simplified):

0 * * * * mysqldump mydb | ifne xz > "/tmp/$(date +\%F).sql.xz" | logger -t mysqldump -p cron.err

In the abova example, I need to send stdout from mysqldump to ifne xz and only if either mysqldump or ifne xz generate error do I need to pipe it to logger.

This syntax needs to works in dash (/bin/sh)

  • 2
    Your edit constitutes a new question. – Kusalananda Jun 6 at 10:41
4
+100

You can use the logger subsystem. There are two variants, depending on whether you have systemd installed or not.

  1. With systemd - using systemd-cat

    echo This is a test with systemd-cat | systemd-cat -t mytest -p info
    

    This writes a message to the journal logger (see journalctl) and also through to the legacy system logging subsystem where you'll find the output in /var/log/syslog, etc.

    journalctl -t mytest
    -- Logs begin at Thu 2020-05-21 07:41:00 UTC, end at Mon 2020-06-01 13:34:56 UTC. --
    Jun 01 13:34:56 pi mytest[24236]: This is a test through systemd-cat
    
  2. With syslog - using logger

    echo This is a test with logger | logger -t mytest -p local0.info
    

    This writes a message through the syslog subsystem (see rsyslog.conf or similar) where you'll find the output in /var/log/syslog, etc.

    tail /var/log/syslog
    [...]
    Jun  1 13:34:56 pi mytest[24236]: This is a test through systemd-cat
    Jun  1 13:38:28 pi mytest: This is a test through logger
    

To use one of these logging options just append to your cron job, adjusting the tag name (myscript) and priority (info) in the example

0 * * * * /path/to/script 2>&1 | systemd-cat -t myscript -p info

Now that you have provided a concrete example, where you want stdout to get written to a target data file but you want to log stderr, you can use this

0 * * * * ( mysqldump mydb | ifne xz > "/tmp/$(date +\%F).sql.xz" ) 2>&1 | logger -t mysqldump -p cron.err
| improve this answer | |
  • I don't have systemd, so I will use logger. But do I have to do this for each script/each line in crontab? Can't I set this globally? Where is the MTA configured in cron? Why does cron look for MTA? Can it be changed, or is it hardcoded? – 400 the Cat Jun 1 at 13:54
  • I assumed you didn't want to install an MTA, because that would have been an obvious solution to the problem (error message, "No MTA installed [...]"). With an MTA you'll get error messages in the local spool directory, /var/mail/$USER – roaima Jun 1 at 14:39
  • thank, that works perfectly. – 400 the Cat Jun 7 at 5:58

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.