Solved

Sending an email with CRON

Posted on 2004-08-26
10
446 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix process listing into CSV format 3 53
centos linux 65 192
php56-php-mcrypt for rhel7 php56 1 111
LINUX Field Separators 7 57
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Fine Tune your automatic Updates for Ubuntu / Debian
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.:
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.

733 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