?
Solved

Sending an email with CRON

Posted on 2004-08-26
10
Medium Priority
?
461 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
[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
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 1000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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, 21 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