【发布时间】:2018-05-24 10:08:56
【问题描述】:
我有一个 bash 脚本,用于检查以确保已创建备份,并且我正在 nagios 中运行它。检查本身工作正常,但 nagios 并没有打印我在 shell 中运行时看到的所有相同输出。
bash 脚本:
#!/bin/bash
# Check whether a specified backup exists
filemask=$1
ls_output=$(smbclient \\\\Server\\Folder$ -U Domain\\Username%password -c ls | grep $filemask)
month=$(echo $ls_output | awk '{print $5}')
day=$(echo $ls_output | awk '{print $6}')
t=$(echo $ls_output | awk '{print $7}')
echo "Last Backup: $month $day $t"
today=$(date +%d)
yesterday=$(( 10#$today - 1 ))
if [ $yesterday -lt 10 ]; then
yesterday="0$yesterday"
fi
if [ $day != $today -a $day != $yesterday ]; then
exit 2
fi
exit 0
当我在 shell 中运行它时,我会得到类似的东西
Last Backup: May 24 11:03:44
但 Nagios 仅显示此输出:
Last Backup:
【问题讨论】:
-
您的命令
smbclient \\\\Server\\Folder$ -U Domain\\Username%password -c ls | grep $filemask我认为返回空输出。检查 $1 内部是否不为空。 -
nagios 是如何运行你的脚本的?通过 bash 还是通过您的默认 shell?此外,请尝试在脚本开头设置
set -x以获取有关变量中哪些值的更多详细信息