Table of contents

Introduction

I was having high CPU usage issues on my Mail-in-a-box server that was caused by SpamAssassin sa-learn script which is a Bayesian classifier that's used to determine which messages should be considered as spam.

htop sa-learn linux
htop view of my mail server

While I understand the importance of this job I couldn't accept the fact that it was making my server slow and unresponsive. After reading the documentation for a few minutes I realized there isn't a quick and easy way to limit the amount of CPU the process is using simply by changing the configuration files.

Looking for solution online

When I started lurking through Google results I stumbled upon this article:

https://support.plesk.com/hc/en-us/articles/12377714583703

In Linux, /bin/nice  is a command used to adjust the priority of a process, effectively controlling how much CPU time it is allocated in relation to other processes.

The nice command is used to launch a program with a specified "niceness" value. The niceness value ranges from -20 to 19, with lower values indicating higher priority (more CPU time) and higher values indicating lower priority (less CPU time). By default, processes have a niceness value of 0.

Here's how you can use the nice command:

nice -n <niceness_value> <command>

Fixing the solution from plesk forum

sa-learn Solution from Plesk website
Solution from Plesk website

When I ran the commands from Plesk forum the solution didn't initially work. I got this error:

/bin/nice: ‘19’: No such file or directory

This is because the nice command expects an executable command as its argument and "19" is not a valid command. "19" should specify the priority level and not be interpreted as a command or file.

Final solution

The final commands with fixed script should look like this:

cp -a /usr/bin/sa-learn{,.orig}
chmod +x /usr/bin/sa-learn

Step by step:

  1. Copy the file /usr/bin/sa-learn to a new file named /usr/bin/sa-learn.orig
  2. Add execute permissions to the script
  3. nano /usr/bin/sa-learn and paste in the script from below
#!/bin/bash
/bin/nice -n 19 /usr/bin/perl -T -w /usr/bin/sa-learn.orig $@
That's what the sa-learn should look like in /usr/bin/sa-learn

And that's it! Now whenever sa-learn process runs it will have the lowest priority compared to other crucial processes in the system, enjoy.