Cron. Daily Exec Format Error

Cron. Daily Exec Format Error

Using Ubuntu 16. I have the following script in cron.daily folder. However I am getting an error via email notification:

/etc/
run-parts: failed to exec /etc/ Exec format error
run-parts: /etc/ exited with return code 1

Script

  #!/bin/bash
    LOGFILE="/var/log/clamav/clamav-$(date +'%Y-%m-%d').log";
    EMAIL_MSG="Please see the log file attached.";
    EMAIL_FROM="";
    EMAIL_TO="";
    DIRTOSCAN="/var/www";

    for S in ${DIRTOSCAN}; do
     DIRSIZE=$(du -sh "$S" 2>/dev/null | cut -f1);

     echo "Starting a daily scan of "$S" directory.
     Amount of data to be scanned is "$DIRSIZE".";

     clamscan -ri "$S" >> "$LOGFILE";

     # get the value of "Infected lines"
     MALWARE=$(tail "$LOGFILE"|grep Infected|cut -d" " -f3);

     # if the value is not equal to zero, send an email with the log file attached
     if [ "$MALWARE" -ne "0" ];then
     # using heirloom-mailx below
     echo "$EMAIL_MSG"|mail -a "$LOGFILE" -s "Malware Found" -r "$EMAIL_FROM" "$EMAIL_TO";
     fi
    done

    exit 0

I can't figure out the problem here. Any ideas?

I tried runnig this on the terminal and got:

./clamscan_daily
Starting a daily scan of /var/www directory.
 Amount of data to be scanned is 427M.
./clamscan_daily: line 15: /var/log/clamav/clamav-2017-10-25.log: Permission denied
tail: cannot open '/var/log/clamav/clamav-2017-10-25.log' for reading: No such file or directory
./clamscan_daily: line 21: [: : integer expression expected

UPDATE 2:

When I run ls -l - I can see the number 2 for this script only. Could that be the problem?

ls -l
total 56
-rwxr-xr-x 1 root root  376 Mar 31  2016 apport
-rwxr-xr-x 1 root root 1474 Oct 31  2016 apt-compat
-rwxr-xr-x 1 root root  355 May 22  2012 bsdmainutils
-rwxr-xr-x 2 root root  751 Oct  4  2016 clamscan_daily
-rwxr-xr-x 1 root root 1597 Nov 26  2015 dpkg
-rwxr-xr-x 1 root root  372 May  6  2015 logrotate

UPDATE #3

ls -l /var/log/clamav
total 388
-rw-r--r-- 1 root   root   233 Sep 15  2016 clamav-2016-09-15.log
-rw-r--r-- 1 root   root   234 Sep 16  2016 clamav-2016-09-16.log
-rw-r--r-- 1 root   root   234 Sep 17  2016 clamav-2016-09-17.log
-rw-r--r-- 1 root   root   234 Sep 18  2016 clamav-2016-09-18.log
-rw-r--r-- 1 root   root   233 Sep 19  2016 clamav-2016-09-19.log
-rw-r--r-- 1 root   root   234 Sep 20  2016 clamav-2016-09-20.log
-rw-r--r-- 1 root   root   233 Sep 21  2016 clamav-2016-09-21.log
-rw-r--r-- 1 root   root   233 Sep 22  2016 clamav-2016-09-22.log
-rw-r--r-- 1 root   root   233 Sep 23  2016 clamav-2016-09-23.log
-rw-r--r-- 1 root   root   232 Sep 24  2016 clamav-2016-09-24.log
-rw-r--r-- 1 root   root   232 Sep 25  2016 clamav-2016-09-25.log
-rw-r--r-- 1 root   root   232 Sep 26  2016 clamav-2016-09-26.log
-rw-r--r-- 1 root   root   233 Sep 27  2016 clamav-2016-09-27.log
-rw-r--r-- 1 root   root   233 Sep 28  2016 clamav-2016-09-28.log
-rw-r--r-- 1 root   root   232 Sep 29  2016 clamav-2016-09-29.log
-rw-r--r-- 1 root   root   234 Sep 30  2016 clamav-2016-09-30.log
-rw-r--r-- 1 root   root   234 Oct  1  2016 clamav-2016-10-01.log
-rw-r--r-- 1 root   root   234 Oct  2  2016 clamav-2016-10-02.log
-rw-r--r-- 1 root   root   234 Oct  3  2016 clamav-2016-10-03.log
-rw-r--r-- 1 root   root   234 Oct  4  2016 clamav-2016-10-04.log
-rw-r--r-- 1 root   root   238 Oct 25 15:06 clamav-2017-10-25.log
-rw-r----- 1 clamav adm   5688 Oct 25 15:27 clamav.log
-rw-r----- 1 clamav adm  13196 Oct 22 06:25 clamav.log.1
-rw-r----- 1 clamav adm   2001 Aug 21 06:25 clamav.log.10.gz
-rw-r----- 1 clamav adm    896 Aug 13 06:25 clamav.log.11.gz
-rw-r----- 1 clamav adm   1894 Aug  7 06:25 clamav.log.12.gz
-rw-r----- 1 clamav adm   1359 Oct 16 06:25 clamav.log.2.gz
-rw-r----- 1 clamav adm   1153 Oct  8 06:25 clamav.log.3.gz
-rw-r----- 1 clamav adm   1869 Oct  2 06:25 clamav.log.4.gz
-rw-r----- 1 clamav adm    898 Sep 24 06:25 clamav.log.5.gz
-rw-r----- 1 clamav adm   1336 Sep 18 06:25 clamav.log.6.gz
-rw-r----- 1 clamav adm   1710 Sep 10 06:25 clamav.log.7.gz
-rw-r----- 1 clamav adm   1373 Sep  4 06:25 clamav.log.8.gz
-rw-r----- 1 clamav adm   1794 Aug 27 06:25 clamav.log.9.gz
-rw-r----- 1 clamav adm  46135 Oct 25 15:26 freshclam.log
-rw-r----- 1 clamav adm  83017 Oct 22 06:25 freshclam.log.1
-rw-r----- 1 clamav adm   6219 Aug 21 06:25 freshclam.log.10.gz
-rw-r----- 1 clamav adm   4737 Aug 13 06:25 freshclam.log.11.gz
-rw-r----- 1 clamav adm   6389 Aug  7 06:25 freshclam.log.12.gz
-rw-r----- 1 clamav adm   6382 Oct 16 06:25 freshclam.log.2.gz
-rw-r----- 1 clamav adm   5228 Oct  8 06:25 freshclam.log.3.gz
-rw-r----- 1 clamav adm   6415 Oct  2 06:25 freshclam.log.4.gz
-rw-r----- 1 clamav adm   4580 Sep 24 06:25 freshclam.log.5.gz
-rw-r----- 1 clamav adm   6596 Sep 18 06:25 freshclam.log.6.gz
-rw-r----- 1 clamav adm   5320 Sep 10 06:25 freshclam.log.7.gz
-rw-r----- 1 clamav adm   6921 Sep  4 06:25 freshclam.log.8.gz
-rw-r----- 1 clamav adm  15812 Aug 27 06:25 freshclam.log.9.gz
11

2 Answers

It may seem like a small thing, but make sure that the #! characters are the first two characters of the script (with no preceding whitespace). To illustrate:

# cat /etc/
  #!/bin/bash
  echo "Hello from $0" >> /tmp/cron.out
#
# run-parts --report /etc/cron.hourly
/etc/
run-parts: failed to exec /etc/ Exec format error
run-parts: /etc/ exited with return code 1
#

(fails); but after removing the leading whitespace

# sed -i 's/^ *//' /etc/
#
# cat /etc/
#!/bin/bash
echo "Hello from $0" >> /tmp/cron.out

runs successfully

# run-parts --report /etc/cron.hourly
# cat /tmp/cron.out
Hello from /etc/
#
2

Does cron run it as root? Make sure that it does.

Right after the format for the time in your crontab, you can add the user.

For example yourscript will run as root at 6 am everyday if you add this to the crontab:

0 6 * * * root /pathtoscript/yourscript

3

Your Answer

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

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.