Link to home
Start Free TrialLog in
Avatar of mahsa2
mahsa2

asked on

using cron to send an automatic email

i want to send an email out automatically every 2nd and 4th monday of the month - i've been told that cron would be the best way to do it, though if anyone has a better/easier way of doing it i'm all ears - the problems i'm having are 1. i'm new to this  2.i can't figure out how to get cron to preform the task every 2nd and 4th monday of the month 3. i'm not exactly sure how to get cron to send an email  
Avatar of Nick Accad
Nick Accad
Flag of Canada image

cron is easy enough is you RTFM :)

man 5 crontab

(this is not a type, the 5 is to read section 5 of the manual)

the problem you have here is that you want to run it every
2nd and 4th monday, not directly possible with crontab,
but you can do it every monday of the month.

basiclly what you want to do is this:

crontab -e (this will open a VI session)
press i
type: 59 23 * * mon cat /var/log/messages | mail -s messageslog myemail@comapnay.com (all on one line)
[shift]ZZ (save and exit)

this will email a copy of /var/log/messages to myemail@company.com every monday at 11:59 pm

the second part is to run it only every-other monday
we put the job in a script and manipulate the script

in crontab, put
59 24 * * mon /usr/local/bin/script.sh

and in script.sh, put the following

if [ -f /etc/second.monday ]
  cat /var/log/messages | mail -s messageslog myemail@company.com
  touch monday
else
  rm monday
fi

chmod a+x script.sh and put it in /usr/local/bin

this script run every monday according to cron
what it does it that it checks if there is a file
called /etc/second.monday, if it exists, it sends the
email, if it does not exist, it create the file second.monday and exits, sort of a toggle on|off switch

hope that helps

-nick
hmm, i made a type, the second crontab entry should be

59 23 * * mon /usr/local/bin/script.sh

NOT

59 24 * * mon /usr/local/bin/script.sh

sorry about that
-nick
Avatar of mahsa2
mahsa2

ASKER

thanks naccad!!
that's so helpful i have one question though-
what about the months that have 5 mondays? wouldn't that cause the program to send out emails the 1st and 3rd monday on the month after the month with 5 mondays?

also just want to make sure i understand the script.sh -basically it checks to see if the file second.monday is present -if it is it sends the email and removes the file so that the following monday the file won't be present and the program will create the file and exit - no?
ASKER CERTIFIED SOLUTION
Avatar of Nick Accad
Nick Accad
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thx for the points, and here is a modified script

#!/bin/sh
#

if [ -f /etc/4monday ]; # did we already run twice this month?
then
  rm /etc/4monday;
  rm /etc/monday;
  exit 0;
fi

if [ -f monday ]; # does the file monday exist? if no create it
then
  RUNS=`cat /etc/monday`
else
  echo 0 > /etc/monday
  RUNS=0
fi

if [[ $RUNS -eq 2 || $RUNS -eq 4 ]]
then
  echo "MONDAY IS HERE $RUNS" # replace this statement with what u want to run
fi

if [[ $RUNS -eq 4 ]]; # already ran twice?,create 4monday
then
  touch /etc/4monday
fi

RUNS=`expr 1 + $RUNS` # runs=runs+1

echo $RUNS > /etc/monday

-nick