Solved

Sending an email with CRON

Posted on 2004-08-26
10
443 Views
Last Modified: 2013-12-15
I think this is fairly simple to do, but I've never done it before.  And, since I'm new at this I really need some detailed instructions.  Basically, I wan't to set up "cron" to send an email (somone@domainname.com) every Friday morning @ 6:00am PST.  I'm using "qmail" on the Linux (RHE) server.  Can you please help?
0
Comment
Question by:aandrade
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 3

Expert Comment

by:pat5star
ID: 11910524
Assuming your email is a text file located somewhere on disk:

0 6 * * 5 cat /path/to/message.txt | mail -s "Your email subject" -c someone@domain.com,someoneelse@domain.com

-Pat
0
 

Author Comment

by:aandrade
ID: 11919182
I need more detail/step by step instrutions on how to set up and run a cron job.  The above statement makes sense, but where do I put that line?  What command do I use to run it?  etc...

Thanks
0
 
LVL 3

Expert Comment

by:pat5star
ID: 11919259
1) Log into the account you want the email to be sent from.
2) Open up a shell and type crontab -e
3) By default I believe it starts up with the VI editor. Click i to put vi in insert mode and then type out this:

0 6 * * 5 cat /path/to/message.txt | mail -s "Your email subject" -c someone@domain.com,someoneelse@domain.com

(Obviously changing the path to your email file, and setting the subject line you want. You can enter multiple email addresses at the end of that line or just use one...if you do use multiple emails make sure you separate them with comma's like I have in the example above).

4) Press the Esc button and then press ZZ (note they are capitals). This saves your crontab and closes the vi editor.
5) You can confirm it's been entered by typing crontab -l   which will show you what cron jobs you have listed. If you see it with the output of crontab -l then it's set and will run every Friday at 6am.

6) To change your email each week, simply replace/update the file your cron job is pointing too.

Hope that helps,

-Pat
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 4

Expert Comment

by:jonnietexas
ID: 11948625
just put it in /etc/crontab
0
 

Author Comment

by:aandrade
ID: 11964726
I've tried all your (pat5star) suggestions and I still can't get it to work...I'll give more details later this week.
0
 
LVL 2

Expert Comment

by:kevincasey
ID: 11973209
not every system lets arbitrary users install/edit crontabs.  if you use sudo, you could try
sudo crontab -u username -e

of course, you could just run the following as root:
crontab -u username -e

then, pickup at pat4star's third instruction.  one note about #4: when editing crontabs, i use "wq" to write and quit, not "ZZ".  that may just be a system thing, so if one doesn't work try the other.

kevin
0
 

Author Comment

by:aandrade
ID: 11982134
I tried this first and it didn't work: 0 6 * * 5 cat /path/to/message.txt | mail -s "Your email subject" -c someone@domain.com,someoneelse@domain.com

Obviously I substituted the "path", "txt file", and "email" above.

I also tried this in my crontab: 0 3 * * * /home/aandrade/timesheetsReminderScript 1>/dev/null 2>/dev/null

Below is the script I am using:

#!/bin/sh
PATH=/usr/local/qmailanalog/bin:/var/qmail/bin:/bin:/usr/bin:usr/local/bin
EMAILMSG="/tmp/timesheet.$$"

DATE=`date +'%D'`

echo "To: someone@domain.com" > $EMAILMSG
echo "From: postmaster@domain.com" >> $EMAILMSG
echo "Subject: Timesheet Reminder for $DATE" >> $EMAILMSG
echo "" >>$EMAILMSG
echo "" >>$EMAILMSG

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >> $EMAILMSG
cat /home/aandrade/timesheetsReminder.txt > $EMAILMSG
cat $EMAILMSG | qmail-inject

rm -f $EMAILMSG
0
 
LVL 2

Accepted Solution

by:
kevincasey earned 250 total points
ID: 11984098
i'm not too familiar with qmail and the mail program, but here's my two cents.  in the first form of the crontab, try using "mail -s "Your email subject" -c someone@domain.com,someoneelse@domain.com < /path/to/message.txt".  i've never had any luck piping a cat into mailx, but redirecting the standard input has always worked.  [i know there's almost no difference, but there is a difference and your system may or may not work the same way.]

personally, i prefer the second form of the crontab that you have listed.  that way i can write a script for a purpose and have it work a little harder than what i can put in a crontab.  also, i can tweak it without having to use the crontab program.  one note, though, i wouldn't redirect the standard error to /dev/null.  if something doesn't work, you probably want to know about it.  (if cron gets an error on stderr, it e-mails it to root...)  also, if this is being run daily, it might be useful (at first) to leave the file in the /tmp folder and just restrict the permissions on it.  that way, if you do get an error message, you'll be able to at least see what you were trying to send.

i assume it's just a typo here, but you might want to make sure that the line "cat /home/aandrade/timesheetsReminder.txt > $EMAILMSG" is appending to the file instead of overwriting it.  also, the quotes around date won't work as they are listed.  you'll want two back ticks (`) instead of one and an apostrophe.

next, make sure your script actually runs reliably as the user your trying to install the crontab for.  i checked out the man page for qmail-inject; i'd be more likely to use more command line flags and environment variables to set the headers than trying to create them directly in the file.  [i don't have access to qmail either way, so i'm not sure i can help much here.]  also, it would probably be good form to abstract the script a little more.  for example:

#!/bin/sh
PATH=/usr/local/qmailanalog/bin:/var/qmail/bin:/bin:/usr/bin:usr/local/bin
EMAILMSG="/tmp/timesheet.$$"
MAILTO="someone@domain.com"
MAILFROM="postmaster@domain.com"
MAILSUBJECT="Timesheet Reminder for `date +'%D'`"
MAILBODY="/home/aandrade/timesheetsReminder.txt"

echo "To: $EMAILMSG" > $EMAILMSG
echo "From: $MAILFROM" >> $EMAILMSG
echo "Subject: $MAILSUBJECT" >> $EMAILMSG
echo "" >>$EMAILMSG
echo "" >>$EMAILMSG

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >> $EMAILMSG
cat $MAILBODY >> $EMAILMSG
cat $EMAILMSG | qmail-inject

#rm -f $EMAILMSG
0
 
LVL 2

Expert Comment

by:kevincasey
ID: 11984107
n.b.  you did have two backticks around the date command.  my bad.
kevin
0
 

Author Comment

by:aandrade
ID: 11993977
Thanks to everyone for your response and input.  Kevincasey, your suggestion was awesome and your script form is neat and clean.  My mistake was overwritting my file instead of appending to it.  Thanks for pointing that out Kevincasey.  Again, I was impressed with your clean script form.  Thanks to all!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
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 …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

772 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