Solved

Sending an email with CRON

Posted on 2004-08-26
10
441 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
 
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Automating a script for user accounts LINUX 14 67
Block sender e-mail address in Postfix 4 64
linux ssh 4 57
How can i set a Environment Variable in my Unix script ? 3 47
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…
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…
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.:

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now