Hallo liebe Community,
Ich versuche einen Cron-job ans laufen zu bekommen, mit dem mir mein Pi eine Mail schickt, sobald sich seine IP-Adresse geändert hat. Das Skript läuft per Shell einwandfrei, nur der Cronjob tut Nichts. Hier die Infos:
Ausgabe von Crontab -l:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
SHELL=/bin/bash
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /home/pi/scheduledTasks/regularIPMail/sendMail.sh
#Kommentar
Alles anzeigen
Dies sollte jede Minute das sh-Skript ausführen. Die Shell hab ich auf Bash gesetzt, PATH-Variable brauche ich nicht, da ich den absoluten Weg zum Skript angegeben habe.
Hier mein sendMail.sh. Ausführen über die Kommandozeile funktioniert ! (Habe den Mailteil auskommentiert, damit mein Postfach nicht zugespammt wird)
#!/bin/bash
PATH=/usr/bin:/bin:/sbin
ipv6Address=$(ifconfig|grep inet6.*Global) # contains IPv6 address in human readable form
oldIpv6Address=$(cat oldIP.txt)
if [ "$ipv6Address" != "$oldIpv6Address" ]
then
echo "Test"
#IP Adresse hat sich geändert !!!
#schicke Mail
#echo $ipv6Address | mail -s "Raspi hat eine neue Addresse" ****@*****.**
#überschreibe die Datei
echo "$ipv6Address" > oldIP.txt
echo $ipv6Address
echo $oldIpv6Address
else
echo "No IP change detected"
fi
Alles anzeigen
zur PATH-Variable (Wo die ganzen Befehle sind):
ZitatAlles anzeigenpi@raspberrypi:~/scheduledTasks/regularIPMail $ whereis mail
mail: /usr/bin/mail.mailutils /usr/bin/mail /etc/mail.rc /usr/share/man/man1/mail.1.gz
pi@raspberrypi:~/scheduledTasks/regularIPMail $ whereis grep
grep: /bin/grep /usr/share/man/man1/grep.1.gz
pi@raspberrypi:~/scheduledTasks/regularIPMail $ whereis echo
echo: /bin/echo /usr/share/man/man1/echo.1.gz
pi@raspberrypi:~/scheduledTasks/regularIPMail $ whereis ifconfig
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
pi@raspberrypi:~/scheduledTasks/regularIPMail $ whereis cat
cat: /bin/cat /usr/share/man/man1/cat.1.gz
pi@raspberrypi:~/scheduledTasks/regularIPMail $
In der Datei /var/log/syslog steht jede Minute der folgende Eintrag:
ZitatNov 23 18:22:01 raspberrypi CRON[17918]: (pi) CMD (/home/pi/scheduledTasks/regularIPMail/sendMail.sh)
Rechte bei der Skriptausführung (chmod 777):
Zitatpi@raspberrypi:~/scheduledTasks/regularIPMail $ ls -l
total 4
-rwxrwxrwx 1 pi pi 493 Nov 23 17:44 sendMail.sh
Ich habe so langsam keine Idee mehr, warum der Cronjob nicht läuft. Wenn ich das Skript über die Shell ausführe, dann wird sofort die Datei oldIP.txt mit der IPv6 Adresse erstellt. Falls die Datei schon existiert bekomme ich den Output "No IP change detected" und wenn ich einen Fehler in oldIP.txt einbaue wird der auch erkannt. Nun ist die oldIP.txt gelöscht, doch es wird keine Neue erstellt. (obwohl der Cronjob läuft, sieht man ja am syslog). Ich habe vermutlich irgendwas mit der Pfadvariablen übersehen, aber ich komm einfach nicht mehr weiter.
LG treecake