How to setup Spamassasin to run as a deamon?

SpamAssasin is perhaps the most powerful spam filter out there and many companies and projects are using it on a regular basis. I installed SpamAssasin on Ubuntu by running the following command.

sudo apt-get  install spamassassin

There are a number of ways you could run SpamAssasin as and integrate it with either your mail program or custom programs/scripts to check for spam in email's content. In this tutorial, I am going to explain you how to run SpamAssasin as a daemon and use spamc program to check for spam in email's content. spamc works like a client and connects to the spamd daemon program through a port or socket file and prints output. This perhaps is a faster method than starting the spamassasin program each time you have to check for spam.

First of all, you should create a separate user for the deamon to run as. Lets add a Linux group and user "spam" for this purpose:

$ sudo groupadd spam
$ sudo useradd -m -d /home/spam -g spam -s /bin/false spam
$ sudo chmod 0700 /home/spam

On a Ubuntu machine, I updated /etc/default/spamassassin and on the line with OPTIONS added the --socketpath option with an appropriate value. Here is how the line looks like after making the change (and adding some other options at the same time):

OPTIONS="--username=spam --socketowner=spam --socketgroup=spam --socketmode=0606 --socketpath=/home/spam/spamassasin.sock --create-prefs --max-children 5 --helper-home-dir"

Now, you can start (restart if already running) the SpamAssasin daemon.

sudo /etc/init.d/spamassasin start

Lets try using the daemon to check sample messages (that come with SpamAssasin) for spam. There are two sample files that come with SpamAssasin. I found them at the following locations on my system:

/usr/share/doc/spamassassin/examples/sample-spam.txt
/usr/share/doc/spamassassin/examples/sample-nonspam.txt

I ran the following commands to check the contents of the files above.

spamc --socket=/vol/spamassasin.sock < /usr/share/doc/spamassassin/examples/sample-spam.txt | grep X-Spam

spamc --socket=/vol/spamassasin.sock < /usr/share/doc/spamassassin/examples/sample-nonspam.txt | grep X-Spam

The first command above will show an X-Spam-Status of "Yes" meaning the file has spam content and the second will have an X-Spam-Status value of "No" indicating that according to SpamAssasin the file is free from any kind of spam.

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I recommend thoroughy testing on a production-like test system first before moving to production.