Is there a way to stop ClamScan eating my server CPU?
5 Answers
Install cpulimit
sudo apt-get install cpulimit
It provides different methods of limiting the CPU usage of a process foo
to say, 20%
By its process-name:
sudo cpulimit -e foo -l 20
.By its absolute path name:
sudo cpulimit -P /usr/bin/foo -l 20
By its PID:
- Find the PID of the process:
pidof foo
. (say, it outputs 1881) sudo cpulimit -p 1881 -l 20
- Find the PID of the process:
-
No target process found... Maybe it's because I have a script to run clamscan? i do: sudo cpulimit -e /etc/cron.hourly/virusscan -l 15– PittoJan 11, 2011 at 14:22
-
@Pitto: You have entered a wrong command,
/etc/cron.hourly/virusscan
is not a process. If you need to limitclamscan
, runsudo cpulimit -e clamscan -l 15
.– SidJan 11, 2011 at 14:32 -
-
2
sudo
is not required the process isn't a system process. Just as a note– AnwarOct 15, 2012 at 3:56 -
This would be a really neat solution if used programattically! Does it support pattern-based searches? Dec 29, 2017 at 19:42
Just as an alternative to cpulimit:
You could start clamscan with the nice-command, e.g.
nice -n 19 clamscan
.
See man nice
for details.
It does NOT limit the CPU, but it does lower the priority of the process.
Also there is renice
to alter the priority of running processes.
-
3
-
5As long as no other process requires cputime, clamscan gets a lot of it. But as soon as another process (which has a higher priority) needs cputime, clamscan has no chance. cpulimit limits absolute cputime, and nice limits relative cputime.– ClausiJan 11, 2011 at 16:06
-
2
-
Are there any tools to define a default nice value for particular applications to run with? Preferably a tool that lets you compare all of your presets side-by-side. Dec 29, 2017 at 19:40
If you're running clamd with systemd, you could use the CPUQuota
option.
Edit /lib/systemd/system/clamav-daemon.service
to include this line in the [Service]
section:
CPUQuota=20%
Then restart the service
sudo systemctl daemon-reload
sudo systemctl reload-or-restart clamav-daemon
This was going to be a comment on Clausi's answer (which I believe is the most "correct" from a system administration viewpoint, in my opinion) but it bloomed into something too big to fit in the comment box.
Clamscan has a fixed amount of work to do so limiting it to a certain speed means it's just going to take longer. It's going to hold the CPU in contention for longer.
Allow it to run as fast as it can means you use your CPU to its fullest. Making it very "nice" means it'll let other processes do their work before its own. This means if there are lots of other busy processes, yes, it'll take a long time to do its own work but if there's nothing on there, it'll just chunk through its workload.
This topic can be useful: HOWTO: Set maximum CPU consumption in percentage by any process