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

  • 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 8: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 8: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 8: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 8:58
  • Basically using `hostname.local' See "Connecting a Computer to the Pi" in How to set up networking/WiFi – Milliways Jun 29 '18 at 9:36
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.

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.

  • 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 6 '18 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.