Koozali.org: home of the SME Server

Cron job - auto email file on schedule/changes

SteveB

Cron job - auto email file on schedule/changes
« on: June 21, 2004, 12:41:38 PM »
Is there an easy way to add a cron job that will email a sepcific file on schedule or preferably just to email the file after changes to it?
Thanks.

schirrms

Cron job - auto email file on schedule/changes
« Reply #1 on: June 21, 2004, 08:01:56 PM »
Hi,

Very easy for a text file, embedded in the mail :

cat /etc/password | mail your@email.add -s"This is a report"

Not so easy if you want a file attachment.
Out of the box, there some perl functions for that on SME, but i don't know if there a mail tool embedded.

For the second part :
Maybe a lot of others solutions, but if your cron script do a ls of the file in a log, and on next run, checks if the modification time as changed, this should work (again very easy with perl ;-) )

HTH,

Offline Gaston94

  • *****
  • 184
  • +0/-0
Cron job - auto email file on schedule/changes
« Reply #2 on: June 22, 2004, 12:26:12 AM »
Hi, you can also handle it with a script shell.
If you want to send something else than pure text, you'll need to go through uuencode.
Here after a quick and dirty script which handle attachement (originaly running on an HP system)
Code: [Select]
#!/bin/sh
# send.sh to mail an attachment with sendmail
#  Gaston 20040304
#
# $1 file to mail
# $2 recipient
# $3 subject of message
#
# check for correct number of arguments
if [ $# -lt 1 ]; then
  echo Usage: send.sh file recipient subject
  exit
fi

PATH_TO_FILE=dirname $1
ATTACHEMENT=basename $1

MAIL_MSG="Report for file ${ATTACHEMENT} change"
SUBJECT="File modified"
DEST=user_name@my_domain.name

MAIL_PROG="/lib/sendmail -t"
UUENCODE=/usr/bin/uuencode

if [ X != X$2 ]
 then
DEST=$2
fi
if [ X != X$3 ]
 then
SUBJECT=$3
fi
if [ X != X$4 ]
 then
MAIL_MSG=$4
fi

##
## first put header stuff into temp file
##
echo From: admin >  /tmp/temp_mail
echo Reply-To: admin@my_domain.name >> /tmp/temp_mail
echo To: ${DEST} >> /tmp/temp_mail
echo Subject: ${SUBJECT} >> /tmp/temp_mail
echo Mime-Version: 1.0 >> /tmp/temp_mail
echo Content-Type: multipart/mixed\; boundary=\"NewPart\" >> /tmp/temp_mail
echo Content-Transfer-Encoding: 7bit >> /tmp/temp_mail
echo Content-Disposition: attachment\; filename=\"body.txt\" >> /tmp/temp_mail

##
## !! the blank lines are mandatories !!
##
echo >> /tmp/temp_mail
echo --NewPart     >> /tmp/temp_mail
echo >> /tmp/temp_mail

##
## Mail Message
##
echo $MAIL_MSG           >> /tmp/temp_mail
echo --NewPart >> /tmp/temp_mail
##
## attach file header
##
echo Content-Type: application/octet-stream >> /tmp/temp_mail
echo Content-Transfer-Encoding: x-uuencode >> /tmp/temp_mail
echo Content-Disposition: attachment\; filename=\"${ATTACHEMENT}\" >> /tmp/temp_mail
echo >> /tmp/temp_mail

##
## encode the file and put it with the header infos and mail messages
##
${UUENCODE} ${PATH_TO_FILE}/${ATTACHEMENT} ${ATTACHEMENT} >> /tmp/temp_mail

##
## now mail the file
##
cat /tmp/temp_mail | ${MAIL_PROG}

*the blank lines are _really_ mandatories*

Regarding the monitoring of the file, either you have a dedicated job for either you can use a "flag_file" and find :
Code: [Select]

find /tmp -type f -newer flag_file -exec send.sh attachement dest subject {} \;

in a cron job

Just a way of looking at the problem (apologize but I do not know about perl way of working :cry: )
Rgds
G.