?
Solved

using cron to send an automatic email

Posted on 2003-02-24
5
Medium Priority
?
230 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
[X]
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
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

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