Solved

Sending an email with CRON

Posted on 2004-08-26
10
438 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

12 Experts available now in Live!

Get 1:1 Help Now