使用 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
答案1
这看起来似乎是一件小事,但请确保#!
字符是脚本的前两个字符(前面没有空格)。举例来说:
# 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
#
答案2
cron 是否以 root 身份运行?确保确实如此。
在您的 crontab 中的时间格式之后,您可以添加用户。
例如,yourscript
如果将其添加到 crontab,则会在每天早上 6 点以 root 身份运行:
0 6 * * * root /pathtoscript/yourscript