每小时 Clamscan cron 脚本读取文件列表失败

每小时 Clamscan cron 脚本读取文件列表失败

我将这段代码放在 /etc/cron.hourly/hourlyclamscan 中。

#!/usr/bin/bash
# Create Hourly Cron Job With Clamscan

# Directories to scan
SCAN_DIR=/home/transmission/Downloads

# Temporary file
LIST_FILE=`mktemp /tmp/clamscan.XXXXXX`

# Location of log file
LOG_FILE=/var/log/clamav/hourly_clamscan.log

# Make list of new files
/usr/bin/find "$SCAN_DIR" -type f -mmin -60 -fprint ${LIST_FILE}
# Scan files and remove infected
/usr/bin/clamscan -i -f ${LIST_FILE} --remove > $LOG_FILE

# If there were infected files detected, send email alert
if [ `cat ${LOG_FILE}  | grep Infected | grep -v 0 | wc -l` != 0 ]
then
echo "$(egrep "FOUND" $LOG_FILE)" | /bin/mail -s "VIRUS PROBLEM" -r [email protected] #####@#####.##
fi
exit

当我从终端运行它时,它没有出现任何错误。

但是,当 cron 运行脚本时,它会向根邮箱发送一个错误:错误:--file-list:无法打开文件 /tmp/clamscan.MLXep5

该文件由 find 创建,归 root 所有(权限 600)。cron 作业也以 root 身份运行,因此我认为权限不应该是问题(或者真的是问题吗?)。

答案1

结果是 SElinux 的问题。

audit2allow -a

返回:

#============= antivirus_t ==============

#!!!! This avc can be allowed using the boolean 'antivirus_can_scan_system'
allow antivirus_t home_root_t:dir read;

并通过输入以下内容解决:

setsebool -P antivirus_can_scan_system 1

答案2

除了你的脚本几乎被破坏之外,我建议你写一些如下内容。

不要使用大写的变量名。只有环境变量才按惯例大写。

不要对二进制文件等使用绝对路径find, mail

#!/usr/bin/bash
# Create Hourly Cron Job With Clamscan

# Directories to scan
scan_dir="/home/transmission/Downloads"

# Temporary file
list_file=$(mktemp /tmp/clamscan.XXXXXX)

# Location of log file
log_file="/var/log/clamav/hourly_clamscan.log"

# Make list of new files
find "$scan_dir" -type f -mmin -60 -fprint "$list_file"
# Scan files and remove infected
clamscan -i -f "$list_file" --remove > "$log_file"

# If there were infected files detected, send email alert
grep -q "Infected" "$log_file" && mail -s "VIRUS PROBLEM" -r [email protected]
exit

相关内容