Ashro-one / Ashro_linux

Linux通用应急响应脚本,适用大多数情况
152 stars 13 forks source link

定时任务目录修正为/var/spool/cron/crontabs/* #1

Open theLSA opened 1 month ago

theLSA commented 1 month ago

问题:定时任务的第469行,使用/var/spool/cron/*无法找到定时任务。

测试过程: debian系统

定时任务: * echo "This is a random message: $(date)" >> /home/user/crontest.txt crontab -l和cat /var/spool/cron/crontabs/user

测试代码:

for user_crontab in /var/spool/cron/crontabs/*; do
        username=$(basename "$user_crontab")
        crontab_content=$(cat "$user_crontab" 2>/dev/null | grep -v '^#' | grep -v '^$')
        if [ -n "$crontab_content" ]; then
            foundUserCrontab=1
            echo "found cron"
            echo -e "\n$username crontab:$crontab_content\n"
        else
            echo "no cron"
        fi
    done

如果代码第一行目录为/var/spool/cron/输出 no cron;目录修改为/var/spool/cron/crontabs/则正常如下图 ashro-issue01

解决:修改为/var/spool/cron/crontabs/*即可(不确定是否兼容其他系统),另外建议加上grep -v '^#' | grep -v '^$过滤掉空行和#注释。

Ashro-one commented 3 weeks ago

谢谢您的提交, 目前添加更改脚本,在第469行到488行代码已经被更新为如下,目前会考虑到

检查 /var/spool/cron 目录:。 检查 /var/spool/cron/crontabs 目录 检查 /etc/crontab 文件: 检查 /etc/cron.d 目录: 检查 /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly 目录: 是否存在,如果存在,遍历其中的所有文件并读取内容。 代码如下:

# 检查 /var/spool/cron 目录
if [ -d "/var/spool/cron" ]; then
    for user_crontab in /var/spool/cron/*; do
        username=$(basename "$user_crontab")
        crontab_content=$(cat "$user_crontab" 2>/dev/null)
        if [ -n "$crontab_content" ]; then
            (echo "[!!!]用户 $username 的定时任务如下:" && echo "$crontab_content") | $Ashro_saveresult
        fi
    done
else
    echo "[!!!]未找到 /var/spool/cron 目录,无法查找用户定时任务" | tee -a "$danger_file" | $Ashro_saveresult
fi

# 检查 /var/spool/cron/crontabs 目录
if [ -d "/var/spool/cron/crontabs" ]; then
    for user_crontab in /var/spool/cron/crontabs/*; do
        username=$(basename "$user_crontab")
        crontab_content=$(cat "$user_crontab" 2>/dev/null)
        if [ -n "$crontab_content" ]; then
            (echo "[!!!]用户 $username 的定时任务如下:" && echo "$crontab_content") | $Ashro_saveresult
        fi
    done
else
    echo "[!!!]未找到 /var/spool/cron/crontabs 目录,无法查找用户定时任务" | tee -a "$danger_file" | $Ashro_saveresult
fi

# 检查 /etc/crontab 文件
if [ -f "/etc/crontab" ]; then
    crontab_content=$(cat /etc/crontab 2>/dev/null)
    if [ -n "$crontab_content" ]; then
        (echo "[!!!]/etc/crontab 定时任务如下:" && echo "$crontab_content") | $Ashro_saveresult
    fi
else
    echo "[!!!]未找到 /etc/crontab 文件,无法查找系统定时任务" | tee -a "$danger_file" | $Ashro_saveresult
fi

# 检查 /etc/cron.d 目录
if [ -d "/etc/cron.d" ]; then
    for cron_file in /etc/cron.d/*; do
        cron_content=$(cat "$cron_file" 2>/dev/null)
        if [ -n "$cron_content" ]; then
            (echo "[!!!]$cron_file 定时任务如下:" && echo "$cron_content") | $Ashro_saveresult
        fi
    done
else
    echo "[!!!]未找到 /etc/cron.d 目录,无法查找系统定时任务" | tee -a "$danger_file" | $Ashro_saveresult
fi

# 检查 /etc/cron.hourly 目录
if [ -d "/etc/cron.hourly" ]; then
    (echo "[!!!]/etc/cron.hourly 定时任务目录存在。") | $Ashro_saveresult
else
    echo "[!!!]未找到 /etc/cron.hourly 目录" | tee -a "$danger_file" | $Ashro_saveresult
fi

# 检查 /etc/cron.daily 目录
if [ -d "/etc/cron.daily" ]; then
    (echo "[!!!]/etc/cron.daily 定时任务目录存在。") | $Ashro_saveresult
else
    echo "[!!!]未找到 /etc/cron.daily 目录" | tee -a "$danger_file" | $Ashro_saveresult
fi

# 检查 /etc/cron.weekly 目录
if [ -d "/etc/cron.weekly" ]; then
    (echo "[!!!]/etc/cron.weekly 定时任务目录存在。") | $Ashro_saveresult
else
    echo "[!!!]未找到 /etc/cron.weekly 目录" | tee -a "$danger_file" | $Ashro_saveresult
fi

# 检查 /etc/cron.monthly 目录
if [ -d "/etc/cron.monthly" ]; then
    (echo "[!!!]/etc/cron.monthly 定时任务目录存在。") | $Ashro_saveresult
else
    echo "[!!!]未找到 /etc/cron.monthly 目录" | tee -a "$danger_file" | $Ashro_saveresult
fi

printf "\n" | $Ashro_saveresult