Post.Office & Virex Daemon

Post.Office Tips & Tricks: Index | Virex Script Instructions

It's finally possible to run the Post.Office Virex Script as a daemon / background process to continuously scan your mailbox directory. This eliminates the 1 minute delay between executions that was inevitable when running the script through a cron job. Now this script should be able to scan 99% of all new messages that arrive on your server. The only risk remaining is that a message arriving in one second is downloaded the next second already before it could be scanned.

The daemon script is actually quite simple, however I had to change the internal workings of virex scanning script to be workable in a constant scanning environment. You need version 0.8 or later of the virex scanning script.

Here's now the daemon script. You only need to change 2 settings in the script so it knows where to find the preference file and the actual Virex script. If you are using a standard installation of the Virex script, then you don't have to change anything at all.

#!/bin/sh
# Post.Office Anti-Virus Solution - Daemon Script
# Copyright by Joe Savelberg -- jmsc@euregio.net
# version 0.9 -- 2004-03-15 @ 17:30 GMT
# http://www.euregio.net/joe/postoffice/
# Use at own risk. Virex does delete viruses
#
# Install this script in your root home directory
# chmod 755 to make it executable


# Decide where you want to put the preference file. You can edit the preference
# file while the daemon is running, e.g. if you want to change the wait period
# between iterations or if you want to stop the daemon.

preffile="/etc/virexdaemon.conf"

# Tell the script about the name of your anti-virus script. That script
# will be executed by this daemon. Make sure the path is correct.

virexscript="/var/root/virexscript.sh"


# If the preference file does not exist, the script will create it in the
# location you specified above. Default preferences will be written to that
# new preference file.


if [ ! -f $preffile ]
then
echo "Creating preference file in $preffile"
echo "########################################################
# Post.Office Anti-Virus Solution - Daemon Preferences #
# Copyright 2004 by Joe Savelberg - jmsc@euregio.net   #
#                                                      #
# run=[true|false] enables or disables the daemon      #
# wait=[0-9] the number of seconds to wait between     #
#            iterations of the scanning script. It     #
#            depends on your server speed but 5 sec    #
#            should work quite good and 2 works as     #
#            well on a fast server.                    #
########################################################

run=true
wait=2
debug=false

"
> $preffile
fi


# This is the actual daemon loop which will execute your Anti-Virus
# script and wait between iterations as specified by wait=[0-9]


scan=`grep "run=" $preffile | grep -v "#" | cut -d "=" -f 2`
debug=`grep "debug=" $preffile | grep -v "#" | cut -d "=" -f 2`

if [ -z $debug ] ; then
    debug=false
fi
if [ $debug ] ; then
    echo "`date` [$$] Starting Virex Daemon" >> /var/log/virexdebug.log
fi

while $scan
do
        scan=`grep "run=" $preffile | grep -v "#" | cut -d "=" -f 2`
        if [ $debug ] ; then
            echo "`date`" >> /var/log/virexdebug.log
            $virexscript >> /var/log/virexdebug.log 2>&1
        else
            $virexscript
        fi
        mypause=`grep "wait=" $preffile | grep -v "#" | cut -d "=" -f 2`
        sleep $mypause
done
#end

Here is the script again which is much easier to copy to your text editor...

Installation:

  1. download the default daemon script
  2. change the preffile and virexscript lines
    this should reflect the location of your preference file and the actual virex scanning script.
  3. copy the virexdaemon.txt file to the top level of your server
  4. open your Terminal application
    /Applications/Utilities/Terminal.app
  5. change to the root user by typing the following after root#
    su -
  6. move virexdaemon.txt from the top level to the root home directory
    mv /virexdaemon.txt ~/virexdaemon.sh
  7. go to the root home directory
    cd ~root
  8. change the file permissions of virexdaemon.sh to make it executable
    chmod 755 virexdaemon.sh
  9. change the file ownership of virexdaemon.sh
    chown root:wheel virexdaemon.sh
  10. start the virexdaemon as a background process (don't forget to put the ampersand [&] at the end of the command)
    /var/root/virexdaemon.sh &
  11. modify the Post.Office startup item so it will launch the Virex daemon after a reboot
    echo "/var/root/virexdaemon.sh &" >> /Library/StartupItems/PostOffice/PostOffice
  12. All done.

The daemon script now executes the virex scanning script continuously and thereby scans any new messages that arrive on your server. You may want to disable the debug logging feature in your virexscript.sh file because it writes too much information to the virex log file (such as script startup, script end and other informational notices). This can quickly add up when you execute the script through the daemon every 2-5 seconds.

Preferences:

When the script is running, it will always check the preference file to see if any of the settings have been changed. You can edit the preference file any time you'd like using your favorite (command line) text editor.

This allows you to stop the daemon by just editing the preference file and setting "run=false" or you can change the wait period to the number of seconds you'd like.


Please support the development of these scripts through PayPal. Thank you.


Copyright by Jochen "Joe" Savelberg - © 2003-2004 -- Last Modified: 11th February 2004