【发布时间】:2014-08-03 12:17:16
【问题描述】:
当我尝试将以下基本脚本作为 cronjob 运行时,它返回的结果与我手动运行时不同。
当我手动运行它时返回“OK...”当我将它作为 cronjob 运行时它返回“WARNING...”
#!/bin/bash
#
# This script will check the public IP address of your server and compare it against a recent check
# The purpose of this script is to notify you when your public IP address has changed for remote access purposes
#
# Start by defining a couple variables (One checks the last IP, one checks the current)
#
last_ip=$(more /tmp/last_ip_check.txt)
current_ip=$(curl -s ifconfig.me)
date=$(date)
#
#
if [ "$last_ip" == "$current_ip" ]
then
echo "$date OK: Your IP address hasn't changed" >> /tmp/ip_address_changes
else
echo "WARNING: Your IP address has changed to $current_ip" | mailx -s "Plex IP Address Change" emailaddress@domain.com
echo "$date WARNING: Your IP address has changed to $current_ip" >> /tmp/ip_address_changes
fi
#
# Dump the output of your ip check into the /tmp file for the next check
#
echo "$current_ip" > /tmp/last_ip_check.txt
#
我已经获得了 bash_profile 的来源,并在没有运气的情况下向脚本添加了不同的路径。另请注意,last_ip 和 current_ip 是相同的字符串/地址。
【问题讨论】:
-
你为什么在
$(...)中使用寻呼机more而不是$(cat /tmp/last_ip_check.txt)? -
尝试将
set -x放在脚本的开头,以便在执行时输出所有命令。然后检查您的电子邮件以获取输出。 -
当您使用 cron 时,curl 很可能不在您的路径上。指定完整路径或在脚本中设置路径。