procmail
[Top] [All Lists]

Re: Periodically mail procmail's log, without cron?

1999-05-25 00:24:19
Back on April 19, Marek Jedlinski wrote,

J> If the solution is already available somewhere, please tell me to RTFM... a
J> pointer would be nice :) I've looked through Jari's procmail tips, but
J> can't find it there.

I responded at the time,

T> It's not in any fluffy manual I know of, but I have this very thing set up
T> at my ripco.com account, where users do not have cron or at access.

T> Some day I'll have to put my code up for public access.

Well, it's high time I did.  Here's the relevant code from my .procmailrc
there.

First note: my .forward there reads like this:

 "|/home/foley/dattier/bin/procmail DOTFWD=yes || /usr/bin/rmail real-dattier"

so that the DOTFWD variable will distinguish procmail instantiations called
from the MTA from those started interactively from the shell or invoked by
mailx on my outgoing mail.  On other sites where I have accounts with cron
access, it also distinguishes them from procmail processes run from my
crontab or from at jobs.

Second note: the .stored_day file contains this (today being May 23):

STORED_DAY='May 23'

Bad luck, I guess, to be answering in May; normally it has the three-day
abbreviation for the month, but in May that's the same as the full name.

Anyhow, it includes the variable name and the equal sign so that the
assingment can be made with an INCLUDERC call; if it contained only the
value, we'd need this in .procmailrc:

  STORED_DAY="`cat thefile`"

which would fork cat unnecessarily.  (Cats should be forked only when you're
ready to test whether they're tender.)

Third note: `job' is the name of a script in a directory in $PATH that sets
up an environment similar to that which .profile and .kshrc do for my inter-
active logins and then has pdksh eval the rest of the line; newday is the
script I want to be run with my first incoming message of every calendar day.

So, with comments added, here we go:

# If DOTFWD is true, lock regional lockfile and read $STORED_DAY:

:0
* DOTFWD ?? ^[yt]|^on|[1-9]
{ LOCKFILE=../.stored_day.lock
  INCLUDERC=../.stored_day

# Extract three-day month abbreviation and day of month from From_ line:

  :0D
  * ^^From +[^ ]+ +(Sun|Mon|Tue|Wed|Thu|Fri|Sat) \/... +[0-9]+
  { TODAY="$MATCH" }

# If the date in the message and the stored date do not match, run the daily
# job, and have it refresh the name of the logfile:

  :0Di
  * $ ! TODAY ?? ^^()$STORED_DAY()^^
  LOGFILE=| job newday

# The nasty part is that one thing that the newday script does is truncate
# the procmail logfile and mail it to me (and Marek wanted the same thing, so
# it applies here); because procmail has directed stderr and log entries to
# the inode where $LOGFILE pointed when I assigned it above, I have to start
# a new logfile under the same name.  The best way to do that, I found, was
# to have the newday script echo the name of the logfile as its stdout and to
# capture that in the procmailrc recipe as the new logfile -- same value for
# the variable, but new inode for the new day's logfile.  I don't remember
# why this didn't work:
#
# :0Diw
# * $ ! TODAY ?? ^^()$STORED_DAY()^^
# | job newday
# :0a
# { LOGFILE=$LOGFILE }
#
# but for some reason I ended up invoking newday and repointing the LOGFILE
# that way.  It may have had something to do with logging the arrival of the
# piece of email that contained the previous day's assorted logs; if I had
# the `w' flag on the recipe it would come before procmail proceeded to the
# `a' clause and its delivery would be logged to the limbo of the old inode,
# and if I didn't use `w' the reassignment of the LOGFILE variable would occur
# while the old logfile still existed, again losing the entry for the delivery
# of the message with the old logs and also the entry for the message that
# triggered the newday script.

# In order to get a logabstract (was this ever changed?), spawn a clone and
# kill it:

  :0ac
  { LASTFOLDER='job newday'
    HOST }

# Just in case it does get changed, make sure this next one is not abstracted:

  LOGABSTRACT=no

# If the conditions were right for running newday, update the stored date:

  :0Aci
  | echo "STORED_DAY='$TODAY'" > ../.stored_day

# Reset $LOGABSTRACT and release the regional lockfile:

  LOGABSTRACT=all
  LOCKFILE
}

<Prev in Thread] Current Thread [Next in Thread>
  • Re: Periodically mail procmail's log, without cron?, David W. Tamkin <=