web-dev-qa-db-ja.com

Cron.daily Exec形式エラー

Ubuntu 16を使用しています。cron.dailyフォルダーに次のスクリプトがあります。ただし、電子メール通知でエラーが発生しています:

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

脚本

  #!/bin/bash
    LOGFILE="/var/log/clamav/clamav-$(date +'%Y-%m-%d').log";
    EMAIL_MSG="Please see the log file attached.";
    EMAIL_FROM="[email protected]";
    EMAIL_TO="[email protected]";
    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

ここで問題を理解することはできません。何か案は?

私は端末でこれを実行してみましたが、次のようになりました:

./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

更新2:

ls -lを実行すると、このスクリプトの2番のみが表示されます。それが問題でしょうか?

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

更新#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
2
Joanna Mikalai

小さなことのように思えるかもしれませんが、#!文字がスクリプトの最初の2文字であることを確認してください(先行する空白はありません)。説明する:

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

(失敗);しかし、先頭の空白を削除した後

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

正常に実行されます

# run-parts --report /etc/cron.hourly
# cat /tmp/cron.out
Hello from /etc/cron.hourly/Shebang-test
#
6
steeldriver

Cronはrootとして実行しますか?確認してください。

Crontabの時刻の形式の直後に、ユーザーを追加できます。

たとえば、これをcrontabに追加すると、yourscriptは毎日午前6時にrootとして実行されます。

0 6 * * * root /pathtoscript/yourscript

0
Academiphile