This is an old revision of the document!


This guide shows how to configure a script to send it's output via email. Thereby that the MAILTO= setting in crontabs is global, it's not that easy to send an email to a different address from another crontab files. This function/script can be used to send the output of a crontab to a different address:

/path/to/script

#!/bin/bash
RECIPIENT=user@example.com
 
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SCRIPT_NAME=$(basename "$0")
SENDER=root@$(/bin/hostname)
 
crontab () {
        echo "From: $SENDER"
        echo "Subject: <$SENDER> $SCRIPT_DIR/$SCRIPT_NAME "
        echo "To: $RECIPIENT"
 
	# Commands to be run here
 
        echo ""
        echo "Script location: $SCRIPT_DIR/$SCRIPT_NAME"
}
 
crontab | sendmail -t $RECIPIENT
  • linux/linux-tutorials/send-command-output-via-email.1757344161.txt.gz
  • Last modified: 2025/09/08 17:09
  • by Zyzonix