{"id":1053,"date":"2013-07-02T04:26:04","date_gmt":"2013-07-02T10:26:04","guid":{"rendered":"http:\/\/blog.supportpro.com\/?p=1053"},"modified":"2026-03-25T03:00:59","modified_gmt":"2026-03-25T09:00:59","slug":"automating-linux-anti-virus-using-clamav-and-cron","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/automating-linux-anti-virus-using-clamav-and-cron\/","title":{"rendered":"How to Automate ClamAV Virus Scans with Cron Jobs on Linux ?"},"content":{"rendered":"\n<p><strong>Clam AntiVirus (ClamAV)<\/strong> is a free, open-source, cross-platform antivirus toolkit designed to detect various types of malware, including viruses and trojans. It is widely used on mail servers for scanning incoming emails and is compatible with multiple operating systems such as Linux, BSD, macOS, Solaris, and more.<\/p>\n\n\n\n<p>In this guide, we\u2019ll walk through how to install ClamAV and automate virus scanning using cron jobs on a Linux server (tested on Red Hat Enterprise Linux).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Install ClamAV<\/strong><\/h3>\n\n\n\n<p>Install ClamAV and required components using the package manager:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum install -y clamav clamav-db clamd<\/code><\/pre>\n\n\n\n<p>Start the ClamAV daemon:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl start clamd<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>This setup also enables automatic virus definition updates using <code>freshclam<\/code>.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Create a Daily Cron Job for Scanning<\/strong><\/h3>\n\n\n\n<p>To automate scanning, create a script inside the cron daily directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi \/etc\/cron.daily\/clamscan_daily<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Add the Scan Script<\/strong><\/h3>\n\n\n\n<p>Paste the following script into the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash# Email configuration<br>EMAIL=\"alert@domain.com\"<br>SUBJECT=\"ClamAV Alert on $(hostname)\"# Log file<br>LOG=\"\/var\/log\/clamav\/daily_scan.log\"# Run ClamAV scan<br>clamscan -r \/ \\<br>--exclude-dir=\"^\/proc\" \\<br>--exclude-dir=\"^\/sys\" \\<br>--exclude-dir=\"^\/dev\" \\<br>--quiet \\<br>--infected \\<br>--log=$LOG# Check for infections<br>if grep -q \"Infected files: &#91;1-9]\" $LOG; then<br>    MAILFILE=$(mktemp \/tmp\/clamav_alert.XXXXXX)    echo \"To: $EMAIL\" &gt;&gt; $MAILFILE<br>    echo \"From: alert@domain.com\" &gt;&gt; $MAILFILE<br>    echo \"Subject: $SUBJECT\" &gt;&gt; $MAILFILE<br>    echo \"\" &gt;&gt; $MAILFILE<br>    echo \"Malware detected on $(hostname). See details below:\" &gt;&gt; $MAILFILE<br>    echo \"\" &gt;&gt; $MAILFILE<br>    tail -n 50 $LOG &gt;&gt; $MAILFILE    sendmail -t &lt; $MAILFILE<br>fi<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4: Set Proper Permissions<\/strong><\/h3>\n\n\n\n<p>Make the script executable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x \/etc\/cron.daily\/clamscan_daily<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How It Works<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The script runs automatically once a day via cron<\/li>\n\n\n\n<li>ClamAV scans the entire system while excluding critical directories<\/li>\n\n\n\n<li>Scan results are saved to a log file<\/li>\n\n\n\n<li>If any infected files are detected, an email alert is sent<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h3>\n\n\n\n<p>Automating ClamAV scans using cron ensures continuous protection for your server without manual intervention. This setup helps detect threats early and provides instant alerts, improving overall system security.<\/p>\n\n\n\n<p>For advanced configuration or server security support, you can reach out to <a href=\"https:\/\/www.supportpro.com\/requestquote.php\">SupportPRO Server Admin<\/a> for expert assistance.<span id=\"hs-cta-wrapper-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-wrapper\"><span id=\"hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-node hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\"><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/9d590242-d641-4383-94b4-8cfd62f0af6b\"><\/a><\/span><\/span><\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile is-vertically-aligned-center has-white-background-color has-background\"><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\">Facing issues? <\/p>\n\n\n\n<p class=\"has-large-font-size\">Our technical support<br>engineers can solve it. <\/p>\n\n\n\n<!--HubSpot Call-to-Action Code --><span class=\"hs-cta-wrapper\" id=\"hs-cta-wrapper-3350a795-db50-482f-9911-301930d1b1be\"><span class=\"hs-cta-node hs-cta-3350a795-db50-482f-9911-301930d1b1be\" id=\"hs-cta-3350a795-db50-482f-9911-301930d1b1be\"><!--[if lte IE 8]><div id=\"hs-cta-ie-element\"><\/div><![endif]--><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/3350a795-db50-482f-9911-301930d1b1be\" ><img decoding=\"async\" class=\"hs-cta-img\" id=\"hs-cta-img-3350a795-db50-482f-9911-301930d1b1be\" style=\"border-width:0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/3350a795-db50-482f-9911-301930d1b1be.png\"  alt=\"Contact Us today!\"\/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '3350a795-db50-482f-9911-301930d1b1be', {\"useNewLoader\":\"true\",\"region\":\"na1\"}); <\/script><\/span><!-- end HubSpot Call-to-Action Code -->\n<\/div><figure class=\"wp-block-media-text__media\"><img fetchpriority=\"high\" decoding=\"async\" width=\"904\" height=\"931\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png\" alt=\"guy server checkup\" class=\"wp-image-12943 size-full\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png 904w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-291x300.png 291w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-768x791.png 768w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-585x602.png 585w\" sizes=\"(max-width: 904px) 100vw, 904px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Clam AntiVirus (ClamAV) is a free, open-source, cross-platform antivirus toolkit designed to detect various types of malware, including viruses and trojans. It is widely used on mail servers for scanning&hellip;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1053","post","type-post","status-publish","format-standard","hentry","category-miscellaneous"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=1053"}],"version-history":[{"count":10,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1053\/revisions"}],"predecessor-version":[{"id":16562,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1053\/revisions\/16562"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}