首页 > 解决方案 > mdadm 状态改变警报脚本

问题描述

我有一个手工制作的 NAS,运行在“Debian 10 buster”上,里面有 RAID 5,

root@fox-nas:~# uname -a
Linux fox-nas 4.19.0-8-amd64 #1 SMP Debian 4.19.98-1 (2020-01-26) x86_64 GNU/Linux

我尝试设置有关更改 RAID 状态的通知,我创建了一个包含以下内容的脚本:

#!/bin/bash
wirepusher_id="your_id_here"
sr="/root/raid_status_monitor" #script_root
m_d="/dev/md0" #monitored_device
n_s=$(mdadm --detail "$m_d" | awk '/^State/ {printf "%s: ", $1}; /State / {print $NF}')
s_f=""$sr"/status.txt" #status_file
if [ ! -f "$s_f" ]; then
    mdadm --detail "$m_d" > "$s_f"
else
    o_f=$(awk '/^State/ {printf "%s: ", $1}; /State / {print $NF}' "$s_f")
fi
if [ "$n_s" != "$o_f" ]; then
    echo "RAID status changed"
    mdadm --detail "$m_d" > "$s_f"
    echo "$o_f" | tee "$sr"/raid.log
    curl -s "https://wirepusher.com/send?id=$wirepusher_id&title=$HOSTNAME $(hostname -I) $(curl -s 2ip.ru)&message=State of $m_d changed old $o_f new $n_s&type=RAID ALERT" -X POST
fi

它运行时没有任何问题,如 shell 预期的那样,但是当我从 cronjob root 运行它时,每次执行都会收到这封电子邮件。

/root/raid_status_monitor/raid_status.sh: line 4: mdadm: command not found

cronjob 看起来像这样

root@fox-nas:~# crontab -l | grep -v "#" | grep raid
@hourly bash /root/raid_status_monitor/raid_status.sh

谁能帮我找出为什么会这样?

标签: bashcrondebianraid

解决方案


确保在 crontab 定义的 $PATH 变量中包含的文件夹中有 mdadm 和 curl 命令。

您还可以检查这些命令在哪里使用它们在脚本中的完整路径:

which mdadm curl

推荐阅读