procmail
[Top] [All Lists]

Re: locking problem?

1999-05-25 15:24:15
Matthew Saroff <saroff(_at_)poseidon(_dot_)vs(_dot_)lmco(_dot_)com> writes:
      I have the following recipe set up to send urls in the body of the
message through lynx and mail it back to me.
      It seems from the results that the lock file is not working, as
occasionally I get back messages that have a couple of pages tacked together.

Perhaps the getweb script is being run through some path other than
this procmail recipe?  I would suggest you move the locking into the
script itself (see below).


recipe follows:
===================================================================
#my own web-email system
:0
*   ^Subject:[  ]##################
* ! ^X-Loop: msaroff(_at_)pca\(_dot_)net
 {
  LOCKFILE=getweb.lock

  :0h #
  backto=|formail -r|formail -x to

  #mail results back to person
  :0b # regional lockfile is still in effect
  | $HOME/getweb/getweb $backto

  LOCKFILE # release regional lock
 }

There's no need for multiple formail processes.  Using two also forces a
shell, so the above takes three processes where one would do.

        :0
        *   ^Subject:[  ]##################
        * ! ^X-Loop: msaroff(_at_)pca\(_dot_)net
        {
            :0 h
            backto=|formail -rtxTo:

            :0 b
            | $HOME/getweb/getweb $backto
        }



============================================
here is the file
cd $HOME/getweb
cat >$HOME/getweb/webpages
from=$1
ans=1;
while [ $ans != 0 ]; do
url=`head -1 $HOME/getweb/webpages`
tail +2 $HOME/getweb/webpages >$HOME/getweb/moo
cp $HOME/getweb/moo $HOME/getweb/webpages
/usr/bin/lynx -dump $url >webit
(echo "From msaroff(_at_)pca(_dot_)net";\
echo "X-Loop: msaroff(_at_)pca(_dot_)net";\
echo "Subject: ${url}";\
echo "To: ${from}";\
echo "MIME-version: 1.0";\
echo "Content-type: text/plain; charset=us-ascii";\
echo "Content-transfer-encoding: 7BIT"
echo "";\
cat $HOME/getweb/webit;)|/usr/bin/sendmail -t
ans=`ls -al $HOME/getweb/webpages |/usr/bin/awk '{print $5}'`
done

I would probably rewrite the above as follows:

        #!/bin/sh
        PATH=/usr/bin:$PATH
        export PATH
        cd $HOME/getweb
        from=$1
        lockfile webit.lock             # protect the webit temp file
        while read url                  # read directly from stdin
        do
            case "$url" in
            *[a-z0-9/]*) ;;
            *)   continue;;             # Skip empty lines
            esac
            if lynx -dump "$url" >webit 2>/dev/null
            then
                # Don't send a message unless lynx was successful
                cat - <<-HEADER webit | sendmail -t
                    To: ${from}
                    Subject: ${url}
                    X-Loop: msaroff(_at_)pca(_dot_)net
                    Mime-Version: 1.0
                    Content-Type: text/plain; charset=us-ascii

                    HEADER
            fi
        done
        rm -f webit webit.lock

Changes:
1) Do the locking here
2) Don't use a temp file to hold the list of urls
3) Skip empty lines
4) Notice when lynx fails and avoiding sending the mail when it does
5) Use a single cat and here-doc to create the header and join it to the body

If you were feeling paranoid or fancy, you could give lockfile
arguments to only retry a fixed number of times before giving up and
aborting the entire script.  Say, you only want to try 10 times (at 8
seconds (the default) each that's 80 seconds max) and locks older than
5 minutes will be considered stale and removed:

        lockfile -r 10 -l 600 webit.lock || exit 1


Philip Guenther

<Prev in Thread] Current Thread [Next in Thread>