Hello,
I created a bash script to send the mainlog.1 as an e-mail attachment for archiving purposes right after midnight when the mainlog refreshes. When I execute the cronjob manually in terminal it works but it doesn’t when enabled in the system cronjobs. How can I implement this in a secure way? Thanks in advance.
- I created the following script:
/etc/exim4/send_exim_log.sh
#!/bin/bash
# Define variables
LOG_DIR="/var/log/exim4"
EMAIL="[email protected]"
YESTERDAY_DATE=$(date -d "yesterday" +%Y-%m-%d)
SUBJECT="Exim4 Log File - $YESTERDAY_DATE"
BODY="Attached is the Exim4 log file for $YESTERDAY_DATE."
ATTACHMENT="/tmp/exim4_log_$YESTERDAY_DATE.txt"
LOG_FILE="$LOG_DIR/mainlog.1"
# Check if the log file exists
if [ ! -f "$LOG_FILE" ]; then
echo "Error: Yesterday's log file not found."
exit 1
fi
# Copy the log file to a temporary location
cp $LOG_FILE $ATTACHMENT
# Send the email with the log file as attachment using mail
echo $BODY | mail -s "$SUBJECT" -A $ATTACHMENT $EMAIL
# Optionally, clean up the temporary file
rm $ATTACHMENT
- Made it executable:
sudo chmod +x /etc/exim4/send_exim_log.sh
- Created a cronjob through HestiaCP under the admin account.
sudo /etc/exim4/send_exim_log.sh
5 / 0 / * / * / *