How to change cron job "subject"
Hi, i am running some cron jobs, i am monitoring the output, but the email subject is in this format.
Cron <username@server> [cron command]
How can i change that, i really dont want my username, server/host name or cron command to be showing up in email, especially as the subject line. I would like to change the subject and set it to be a generic subject.
According to AI, it appears that cron does not control the subject, its the email config, is that correct?.
Here is an interesting idea.... turn the email off for the cron job and have the job file itself send an email using php mail command. This way the subject, from, and header info can be controled. That is thinking outside the box and i like the idea. The downside is that getting an email every time cant be turned off without editing the job file.
Unless there is a way to change this within WHM then ill go with this idea.
Thanks :)
-
Hey there! Yes, that's exactly what I would do - make the cron job call an email so you have more control over those settings, as there isn't going to be an easier method to adjust those values that I can think of.
0 -
turn the email off for the cron job and have the job file itself send an email using php mail command. This way the subject, from, and header info can be controled.
Here's an example you can use. It's a script that was created to replace the emails cPanel turned off several years ago. It was on the old forum, not sure if it’s still around.
root@srv06 [~]# crontab -l
38 3 * * * /root/bin/root-crontab-additions-srv06-01.sh >>/root/log/root-crontab-additions-srv06-01.sh.log 2>&1
root@srv06 [~]# alias ll
alias ll='ls -al --color=tty'
root@srv06 [~]# ll /root/bin/root-crontab-additions-srv06-01.sh
-rwxr-xr-x 1 root root 3452 Aug 1 2020 /root/bin/root-crontab-additions-srv06-01.sh
root@srv06 [~]# cat /root/bin/root-crontab-additions-srv06-01.sh
#!/bin/bash
# # # Description
# Send an email with the cPanel update log
# Send an email if the server needs a reboot
#
# Script=root-crontab-additions-srv06-01.sh
# Author=Michael
# Email=use website contact
# Website=http://www.inet-design.com/
# License=GPL
# Script repository=none
# Last Edit Date=Thu Jul 23 20:33:40 CDT 2020
#
# Note: Should have used sendmail EOF instead of pipes, but throwaway script...
#
#
# # # crontab entry
# 42 4 * * * << runtime of cPanel {fix-cpanel-perl} update on this server, add 20 minutes to run this script.
# 02 5 * * * /root/bin/root-crontab-additions-srv06-01.sh >>/root/log/root-crontab-additions-srv06-01.sh.log 2>&1
#
# echo "Subject: send mail testsrv06" | sendmail -v root@srv06...
# echo "Subject: send mail testsrv06" | sendmail -v srv06_whm@...
#
# # # Be Very Nice
renice -n 19 -p $$ >/dev/null 2>/dev/null
#
# # # # #
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo "$CurrDateTime: # # # Starting # # #"
# # # Variable Setups
SendMailTo="root@srv06..."
#SendMailTo="srv06_whm@..."
SendMailFrom="Cron Daemon <root@srv06...>"
SendMailSubject="Cron <root@srv06> (/usr/local/cpanel/scripts/fix-cpanel-perl; /usr/local/cpanel/scripts/upcp --cron)"
RebootText=`/usr/sbin/whmapi1 system_needs_reboot`
# # # Derived Variables
# CentOS 6 only has /bin/hostname
# CentOS 7 has both /bin/hostname and /usr/bin/hostname
SendMailBody="Hostname `/bin/hostname`
$RebootText
`cat /var/cpanel/updatelogs/last`"
( echo "To: $SendMailTo"; echo "From: $SendMailFrom"; echo "Subject: $SendMailSubject"; echo "$SendMailBody" ) | /usr/sbin/sendmail "$SendMailTo"
EXITCODE=("${PIPESTATUS[@]}")
TESTVAR="${EXITCODE[0]}"
if [ "$TESTVAR" -ne 0 ] ; then
Message01='UPDATELOGS: Echo had issues: '
Message02="$TESTVAR"
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo "$CurrDateTime: $Message01$Message02"
echo "$CurrDateTime: # # # Exited with Errors # # #"
TESTVAR="${EXITCODE[1]}"
if [ "$TESTVAR" -ne 0 ] ; then
Message01='UPDATELOGS: Sendmail had issues: '
Message02="$TESTVAR"
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo "$CurrDateTime: $Message01$Message02"
echo "$CurrDateTime: # # # Exited with Errors # # #"
fi
fi
RebootFlag=`echo "$RebootText" | grep 'needs_reboot:' | awk '{print $2}'`
if [ "$RebootFlag" -ne 0 ] ; then
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo "$CurrDateTime: Server Needs a Reboot"
SendMailSubject="Cron <root@srv06> Needs Reboot"
SendMailBody="Hostname `/usr/bin/hostname`
$RebootText"
( echo "To: $SendMailTo"; echo "From: $SendMailFrom"; echo "Subject: $SendMailSubject"; echo "$SendMailBody" ) | /usr/sbin/sendmail "$SendMailTo"
EXITCODE=("${PIPESTATUS[@]}")
TESTVAR="${EXITCODE[0]}"
if [ "$TESTVAR" -ne 0 ] ; then
Message01='NEEDS REBOOT: Echo had issues: '
Message02="$TESTVAR"
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo "$CurrDateTime: $Message01$Message02"
echo "$CurrDateTime: # # # Exited with Errors # # #"
TESTVAR="${EXITCODE[1]}"
if [ "$TESTVAR" -ne 0 ] ; then
Message01='NEEDS REBOOT: Sendmail had issues: '
Message02="$TESTVAR"
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo "$CurrDateTime: $Message01$Message02"
echo "$CurrDateTime: # # # Exited with Errors # # #"
fi
fi
fi
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo "$CurrDateTime: # # # Finished # # #"0
Please sign in to leave a comment.
Comments
2 comments