When I try to run the following basic script as a cronjob it returns a different result than when I run it manually.
When I run it manually is returns "OK..." when I run it as a cronjob it returns "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
#
I've sources bash_profile and adding a different path to the script with no luck. Also as a note last_ip and current_ip are the same string/address.
As @Barman said, it works if you change more
with cat
:
Change this line:
last_ip=$(more /tmp/last_ip_check.txt)
To this:
last_ip=$(cat /tmp/last_ip_check.txt)
... and wait for the next execution of the script.
I don't know the reason, but as more
is a pager, I believe cat
is more correct to use in this case.
However, if I test both results and diff
them, I get no differences:
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
Why is that, I don't know...