21

Is there a way to stop ClamScan eating my server CPU?

5 Answers 5

23

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:

    1. Find the PID of the process: pidof foo. (say, it outputs 1881)
    2. sudo cpulimit -p 1881 -l 20
5
  • 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
    – Pitto
    Jan 11, 2011 at 14:22
  • @Pitto: You have entered a wrong command, /etc/cron.hourly/virusscan is not a process. If you need to limit clamscan, run sudo cpulimit -e clamscan -l 15.
    – Sid
    Jan 11, 2011 at 14:32
  • Oh! Right! So I should start cpulimit at startup in rc.local, right?
    – Pitto
    Jan 11, 2011 at 14:51
  • 2
    sudo is not required the process isn't a system process. Just as a note
    – Anwar
    Oct 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
19

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.

4
  • 3
    It looks like it still works eating a lot of cpu...
    – Pitto
    Jan 11, 2011 at 14:41
  • 5
    As 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.
    – Clausi
    Jan 11, 2011 at 16:06
  • 2
    +1. niceing is the traditional way to go, and for good reason.
    – loevborg
    Jan 11, 2011 at 16:13
  • 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
5

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
4

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.

1

This topic can be useful: HOWTO: Set maximum CPU consumption in percentage by any process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.