Bash脚本作为cronjob运行时的不同输出

Bash脚本作为cronjob运行时的不同输出,bash,cron,cron-task,Bash,Cron,Cron Task,当我尝试将以下基本脚本作为cronjob运行时,它返回的结果与手动运行时不同 当我手动运行它时,返回OK。。。当我将其作为cronjob运行时,它会返回警告 #!/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 p

当我尝试将以下基本脚本作为cronjob运行时,它返回的结果与手动运行时不同

当我手动运行它时,返回OK。。。当我将其作为cronjob运行时,它会返回警告

#!/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_概要文件的源代码,并在脚本中添加了一条不同的路径,但运气不好。另外需要注意的是,最后一个ip和当前ip是相同的字符串/地址。

正如@Barman所说的,如果您使用cat进行更多更改,它会起作用:

更改此行:

last_ip=$(more /tmp/last_ip_check.txt)
为此:

last_ip=$(cat /tmp/last_ip_check.txt)
。。。并等待脚本的下一次执行

我不知道原因,但由于more是寻呼机,我相信cat在这种情况下更适合使用

但是,如果我测试这两个结果并加以区分,则不会得到任何差异:

jim@debian:~$ cat /tmp/last_ip_check.txt
1xx.1xx.2xx.1xx
jim@debian:~$ more /tmp/last_ip_check.txt
1xx.1xx.2xx.1xx
jim@debian:~$ cat /tmp/last_ip_check.txt > a
jim@debian:~$ more /tmp/last_ip_check.txt > b
jim@debian:~$ diff a b

为什么,我不知道…

你为什么在$中更多地使用寻呼机。。。相反,如果$cat/tmp/last_ip_check.txt,请尝试将set-x放在脚本的开头,以便它在执行命令时输出所有命令。然后检查您的电子邮件以获得输出。当您使用cron时,curl很可能不在您的路径上。指定完整路径或在脚本中设置路径。谢谢jim和barmar修复了它。我完全把它隔开,并开始假设这是一个环境问题。谢谢你们两位的帮助。因为这是bash,所以您可以节省几微秒的宝贵时间并使用最后一个ip=$