Send exim mainlog.1 as email attachment

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.

  1. 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
  1. Made it executable:
 sudo chmod +x /etc/exim4/send_exim_log.sh
  1. Created a cronjob through HestiaCP under the admin account.
sudo /etc/exim4/send_exim_log.sh
5 / 0 / * / * / *

Hi @chris,

User admin is only allowed to use sudo for scripts inside /usr/local/hestia/bin/

❯ cat /etc/sudoers.d/admin
# Created by hestia installer
Defaults env_keep="VESTA"
Defaults env_keep+="HESTIA"
Defaults:admin !syslog
Defaults:admin !requiretty
Defaults:root !requiretty

# sudo is limited to hestia scripts
admin   ALL=NOPASSWD:/usr/local/vesta/bin/*
admin   ALL=NOPASSWD:/usr/local/hestia/bin/*

So, just copy your script to /usr/local/hestia/bin/ and modify the cron path to your script.

2 Likes

Ah right, thanks a lot @sahsanu! :pray:

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.