1

I've created a script to get my external IP address, check if it's changed and if it has, email it to me letting me know.

Here's the script, aptly named ip.sh (server hostname is "odin"):

#!/bin/sh

# Start by renaming file "current-ip" to "old-ip"
mv -f  /var/www/html/scripts/current-ip  /var/www/html/scripts/old-ip
# Create new "current-ip" with the email's subject line
echo 'Subject: Odin has a new IP address' >> /var/www/html/scripts/current-ip
# Add a blank line
echo '' >> /var/www/html/scripts/current-ip
# Add some text to make the email slightly more readable
echo 'It seems as if a new IP address as been assigned to Odin:' >> /var/www/html/scripts/current-ip
# Get my external IP address and add it to a new line
curl http://ipecho.net/plain -w "\n" >> /var/www/html/scripts/current-ip
# Set permission to make the file readable and writeable
chmod 766 /var/www/html/scripts/current-ip

# Check to see if the newly built "current-ip" matches the old file
if diff /var/www/html/scripts/current-ip /var/www/html/scripts/old-ip >/dev/null ; then
# if it does, do nothing
exit
# Otherwise send that email!
else
  ssmtp -F"Odin" my_emial_address@gmail.com < /var/www/html/scripts/current-ip
fi

So to test this, I go edit the current-ip file to something else and then run the script ./ip.sh and it works like a charm, I get my email from "Odin" telling me what my new IP address is. Fantastic.

So now, I create a symlink:

ln /var/www/html/scripts/ip.sh /sbin/odinip

And test it by running odinip. Works perfectly.

Then I go create the cronjob with crontab -e and enter the following line:

*/5 * * * * /sbin/odinip

I also add the following line to make sure that cron is working:

*/5 * * * * env > /var/www/html/scripts/env.output

I then go edit the current-ip file again to make sure that there will be a difference when the cronjob is run.

Then I wait and wait and wait. The env.output file is created, but no emails are sent.

Checking the syslog (tail -n25 /var/log/syslog) I get:

May  9 13:40:01 odin CRON[7371]: (root) CMD (env > /var/www/html/scripts/env.output)
May  9 13:40:04 odin CRON[7341]: (CRON) info (No MTA installed, discarding output)
May  9 13:40:38 odin crontab[7429]: (root) BEGIN EDIT (root)
May  9 13:40:47 odin crontab[7429]: (root) END EDIT (root)
May  9 13:40:49 odin crontab[7451]: (root) BEGIN EDIT (root)
May  9 13:41:01 odin CRON[7478]: (root) CMD (cd /var/www/html/scripts/ && ./cpuTemp.sh)
May  9 13:41:16 odin rsyslogd-2007: action 'action 17' suspended, next retry is Mon May  9 13:42:46 2016 [try http://www.rsyslog.com/e/2007 ]
May  9 13:41:30 odin crontab[7451]: (root) END EDIT (root)
May  9 13:41:31 odin crontab[7513]: (root) BEGIN EDIT (root)
May  9 13:41:44 odin crontab[7513]: (root) END EDIT (root)
May  9 13:41:46 odin crontab[7548]: (root) BEGIN EDIT (root)
May  9 13:42:01 odin CRON[7587]: (root) CMD (cd /var/www/html/scripts/ && ./cpuTemp.sh)
May  9 13:42:46 odin rsyslogd-2007: action 'action 17' suspended, next retry is Mon May  9 13:44:16 2016 [try http://www.rsyslog.com/e/2007 ]
May  9 13:43:01 odin CRON[7690]: (root) CMD (cd /var/www/html/scripts/ && ./cpuTemp.sh)
May  9 13:43:41 odin crontab[7548]: (root) REPLACE (root)
May  9 13:43:41 odin crontab[7548]: (root) END EDIT (root)
May  9 13:44:01 odin cron[360]: (root) RELOAD (crontabs/root)
May  9 13:44:01 odin CRON[7771]: (root) CMD (cd /var/www/html/scripts/ && ./cpuTemp.sh)
May  9 13:44:18 odin rsyslogd-2007: action 'action 17' suspended, next retry is Mon May  9 13:45:48 2016 [try http://www.rsyslog.com/e/2007 ]
May  9 13:45:01 odin CRON[7849]: (root) CMD (cd /var/www/html/scripts/ && ./cpuTemp.sh)
May  9 13:45:01 odin CRON[7850]: (root) CMD (/home/jim/duckdns/duck.sh >/dev/null 2>&1)
May  9 13:45:01 odin CRON[7851]: (root) CMD (/sbin/odinip)
May  9 13:45:04 odin CRON[7829]: (CRON) info (No MTA installed, discarding output)
May  9 13:45:51 odin rsyslogd-2007: action 'action 17' suspended, next retry is Mon May  9 13:47:21 2016 [try http://www.rsyslog.com/e/2007 ]
May  9 13:46:01 odin CRON[7943]: (root) CMD (cd /var/www/html/scripts/ && ./cpuTemp.sh)

cpuTemp.sh is just another script that I have running there. Maybe something about there being no Mail Transport Agent? But then how does it work when I run it manually?

More info: All commands are run and files edited and crontab edits done as root All of this is happening on a Rasberry Pi 2 running Jessie. VERY recently updated and upgraded.

  • Long ago, I inherited a SuSE box whose cron didn't recognize that its lists are modified until it was restarted. – Gombai Sándor May 9 '16 at 12:57
1

Oh wow. The answer: use full paths

I thought I had done just that, Until I found that someone else had a problem using ssmtp directly from the crontab.

They had:

0 9 * * * ssmtp a@hotmail.com < /home/a/a-msg.txt

and had to change it to

0 9 * * * /usr/sbin/ssmtp a@hotmail.com < /home/a/a-msg.txt

Which apparently fixed the problem.

I, on the other hand was referencing ssmtp from the bash script (ip.sh) with the line:

ssmtp -F"Odin" my_emial_address@gmail.com < /var/www/html/scripts/current-ip

So I updated it to

/usr/sbin/ssmtp -F"Odin" my_emial_address@gmail.com < /var/www/html/scripts/current-ip

Changed, the current-ip contents to force the else of the if statement to fire and it seemed to work. I'll keep testing and update this if I find any other bugs.

| improve this answer | |
0

Cron does not use or remember your environment when executing the commands, therefor paths and other environment variables may not be as you expect.

As you already seem to have found out, the best way to avoid problems is to always use the full path to commands you execute from cron.

| improve this answer | |

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.