?
Solved

using cron to send an automatic email

Posted on 2003-02-24
5
Medium Priority
?
243 Views
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  
0
Comment
Question by:mahsa2
  • 4
5 Comments
 
LVL 3

Expert Comment

by:naccad
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
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
0
 
LVL 3

Expert Comment

by:naccad
ID: 8010261
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
0
 

Author Comment

by:mahsa2
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?
0
 
LVL 3

Accepted Solution

by:
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
comments

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

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.

tc
-nick


0
 
LVL 3

Expert Comment

by:naccad
ID: 8020599
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

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month4 days, 3 hours left to enroll

600 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