using cron to send an automatic email

Posted on 2003-02-24
Medium Priority
Last Modified: 2013-12-15
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  
Question by:mahsa2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4

Expert Comment

ID: 8010240
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
  rm monday

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


Expert Comment

ID: 8010261
hmm, i made a type, the second crontab entry should be

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


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

sorry about that

Author Comment

ID: 8014200
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?

Accepted Solution

naccad earned 300 total points
ID: 8016593
you're right of course *hits head on keyboard*

ok, the 2nd part of the question:
lots of typos in my original post, so ill repeat with

if [ -f /etc/second.monday ]; #does the file exits?
  cat /var/log/messages | mail -s myemail@host.com
  rm /etc/second.monday #delete the file for the next run
  touch /etc/second.monday #look like monday 1 or 3

as for the 1st part of your question, if the month has
five mondays, we'll have to modify the script, a LOT
ill repost at the end of the day when i have a minute.



Expert Comment

ID: 8020599
thx for the points, and here is a modified script


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

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

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

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

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

echo $RUNS > /etc/monday



Featured Post

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question