1

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!!

Salar Yunis
  • 35
  • 2
  • 9
  • I don't have an answer to your question, but why do you think the IP address may change (and frankly why do you care). – Milliways Jun 29 '18 at 08:02
  • I have multiple WiFi Access points at work and sometimes we take it outside for some test and the device keeps changing its IP address, and i always need to know the IP so i can access it – Salar Yunis Jun 29 '18 at 08:15
  • "I … need to know the IP so i can access it" I have 7 Pi but don't know the IP address on any, but this never stops me accessing them. – Milliways Jun 29 '18 at 08:34
  • How do you access them without the IP address, of course without using a screen and a keyboard? – Salar Yunis Jun 29 '18 at 08:58
  • Basically using `hostname.local' See "Connecting a Computer to the Pi" in How to set up networking/WiFi – Milliways Jun 29 '18 at 09:36
  • I had a Pi on a corporate network, all worked well, then on day poof the IP address had changed(they were mucking about with wifi options). So totally get were you are coming from. I had a script that run once the Pi was restarted that sent the IP address to text file in Dropbox via Python if it had changed. Dropbox would then alert me if the file changed. – rob Jun 29 '18 at 10:20
  • Milliways, this is really a good way of accessing the Rpi, thank you!, but i still need to fix the Cron problem, it is very important. – Salar Yunis Jun 29 '18 at 10:41
  • rob, i currently have a script that runs at the beginning and it sends me the IP address, but i have this other script that runs every 5 minutes checking if the IP address has changed and that's why i need it to not run at the first reboot and start running from minute 5, do you have any idea on how to do this ?? – Salar Yunis Jun 29 '18 at 10:45
  • If you just want to know the IP Address your Pi is using don't fiddle with deprecated commands just enter hostname -I on the command line. – Milliways Jun 29 '18 at 12:56

2 Answers2

1

Two suggestions with regard to logic in the script:

1) Parse the output of uptime.

Or

2) Presuming /tmp is a tmpfs backed directory (meaning it disappears when the system shuts down and is created again at reboot; I believe this is the default with Raspbian but you can check this with mount | grep tmpfs, which if it doesn't show /tmp it will show you what directories you could use instead, e.g., /run).

tmpfile=/tmp/checksquiggle  # Arbitrary name
if [ ! -e $tmpfile ]; then
    touch $tmpfile &> /dev/null
    exit 0
fi

The only potential issue with this would be if the cron job runs before /tmp is available -- I would guess that is impossible, but if not it is probably very unlikely and at worst means you will skip ten minutes instead of 5.

Using the tmpfs backed file is more reliable than erasing it via a job at shutdown since the latter disappears no matter how the system stops (power fail, etc.) -- it only exists in RAM. It's also simpler.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
0

There is no way to achieve what you want within cron.

man 5 crontab

You will have to add the appropriate logic to your script.

joan
  • 71,024
  • 5
  • 73
  • 106
  • How is it possible to add this logic, i have edited my question above and added the Bash script i'm using, do you have an idea on how to implement it ?? – Salar Yunis Jun 29 '18 at 11:56
  • You could read /proc/uptime to see how long the system has been up. The first number is seconds of uptime, the second number is seconds of idle time. (I need to check that /proc/uptime exists on Raspbian.) – Chad Farmer Jul 06 '18 at 01:08