I'm trying to run a Bash script with Cron every 5 minutes to check if my Raspberry Pi have changed its IP address or not and update me, this is the script i'm using.
#!/bin/bash
#check and send ip address to email
MYIP=`ifconfig wlan0 | grep 'inet addr'| awk '{print $2}' | cut -d ':' -f 2`;
TIME=`date`;
LASTIPFILE='/var/www/crones/.last_ip_addr';
LASTIP=`cat ${LASTIPFILE}`;
if [[ ${MYIP} != ${LASTIP} ]]
then
# echo "New IP = ${MYIP}"
# echo "sending email.."
echo -e "Hello\n\nTimestamp = ${TIME}\nIP = ${MYIP}\n\nBye" | \
/usr/bin/mail -s "[INFO] New IP" dispositivos@cyclesmartcity.com;
echo ${MYIP} > ${LASTIPFILE};
else
# echo "no IP change!"
echo ${MYIP} > ${LASTIPFILE};
fi
But i want this job to skip the first run when the device is initiated or rebooted since it is overlapping with another script that sends me the IP address at reboot.
I have tried multiple versions of the Cron code that i read online and they both do the job of running every 5 minutes but they keep running at minute 0 when my device is started or restarted.
5,10,15,20,25,30,35,40,45,50,55 * * * * root /bin/bash /var/www/crones/changeip.sh
or:
5-59/5 * * * * root /bin/bash /var/www/crones/changeip.sh
Is there a way in Cron that make the job skip the first reboot run ?? or if i need to modify the Bash script to add this condition, how to do it ?? Thank you!!
hostname -I
on the command line. – Milliways Jun 29 '18 at 12:56