Stijn
Posts: 2
Joined: Sat Jul 18, 2015 6:24 pm

@reboot in crontab

Sat Jul 18, 2015 6:43 pm

Hi all


I'm trying to start a python script, whenever my RasPi reboots. The script only sends me an email, to inform me the pi has restarted.
I've put the line "@reboot sudo python /home/pi/sendmail.py" in crontab, and this won't work... All of the command line is correct, because when I change the line to "* * * * * sudo python /home/pi/sendmail.py", it sends me the email every minute when the pi is on...
So I think there is something wrong with the @reboot command...

any ideas please?
thanks in advance!
Kind regards
Stijn

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: @reboot in crontab

Sun Jul 19, 2015 4:45 am

Stijn wrote:Hi all


I'm trying to start a python script, whenever my RasPi reboots. The script only sends me an email, to inform me the pi has restarted.
I've put the line "@reboot sudo python /home/pi/sendmail.py" in crontab, and this won't work... All of the command line is correct, because when I change the line to "* * * * * sudo python /home/pi/sendmail.py", it sends me the email every minute when the pi is on...
So I think there is something wrong with the @reboot command...

any ideas please?
thanks in advance!
Kind regards
Stijn
Does the script run before networking is up (i.e. the email ends up in the bit-bucket?) If so, try a if-up hook in /etc/network
Does the script really need sudo?

User avatar
rpdom
Posts: 12413
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK

Re: @reboot in crontab

Sun Jul 19, 2015 9:08 am

Have you tried adding logging to the cron to see what errors, if any, are occuring when it runs @reboot?

It is very probably trying to run, but not working, as cron @reboot does work.

Code: Select all

@reboot sudo python /home/pi/sendmail.p@reboot sudo python /home/pi/sendmail.py >/home/pi/reboot.log 2>/home/pi/reboot.err
That will create two files, one with standard output and one with any errors. You could combine the two, but I prefer to keep them separate.

Also, as sprinkmeier said, do you really need sudo just to run a python program to send an email? I doubt it.

User avatar
DougieLawson
Posts: 32376
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: @reboot in crontab

Sun Jul 19, 2015 9:51 am

sprinkmeier wrote: Does the script run before networking is up (i.e. the email ends up in the bit-bucket?) If so, try a if-up hook in /etc/network
Does the script really need sudo?
If you want something run last in the sysvinit scripts then putting it in /etc/rc.local makes more sense than adding it as a /etc/network/if-up.d script. At least that way you're sure it only runs once.

The way I track my public IP address is

Code: Select all

#!/bin/bash
OLDaddr=$(head -1 /home/dougie/ipaddr.saved.txt)
IPaddr=$(printf '\42';dig +short myip.opendns.com \@resolver1.opendns.com | awk '{printf "%s",$0}';printf '\42')

if [ $IPaddr == '"0.0.0.0"' ]; then
  exit 20
fi
if [ $OLDaddr != $IPaddr ]; then
  NOW=$(date +"%d/%m/%Y, %T,")
  printf "$NOW New address found\r\n"
  echo -e $NOW$IPaddr >> /home/dougie/ipaddr.log.txt
  echo $IPaddr | tee /home/dougie/ipaddr.saved.txt
  curl -s -o - --user myDNS-o-MaticID:myDNS-o-MaticPwd  https://updates.dnsomatic.com/nic/update
fi
that script runs from cron every three minutes. When the IP address changes it automatically updates all of my dynamic IP services and cron sends an email to my id because of the print and tee commands in that script.

By adding

Code: Select all

MAILTO=example@gmail.com
to crontab I can get that output [dumped into the spam folder] on my GMail account.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Stijn
Posts: 2
Joined: Sat Jul 18, 2015 6:24 pm

Re: @reboot in crontab

Tue Jul 28, 2015 7:37 pm

Hi all

due to vacation (and my wife not liking me to sit and trial and error with my RasPi every night ;) ), it has taken a while for me to be able te read and reply to all of you. Thanks for all your replies!

I've solved the problem by starting the script from the rc.local file. I just wanted to use Crontab to schedule ALL the scripts, so all schedules would be in only one place. Now I have two places to check... No problem. :lol:


Well, problem's "solved"... thanks again all of you! 8-)
kind regards
Stijn

neetgeek
Posts: 1
Joined: Mon May 14, 2018 11:04 pm

Re: @reboot in crontab

Mon May 14, 2018 11:15 pm

There is more than one cause for this behavior, namely crontab @reboot not working. In my case there was an error in the /etc/init.d/mosquitto script file. Because of this error, the command didn't run, but no indication that it didn't run. However, looking at /var/log/syslog showed there was an error, and some sleuthing enabled me to eliminate the error. The @reboot command does work.

glum
Posts: 15
Joined: Wed May 16, 2018 11:27 am

Re: @reboot in crontab

Wed May 16, 2018 11:52 pm

Stijn wrote:
Sat Jul 18, 2015 6:43 pm

I'm trying to start a python script, whenever my RasPi reboots. The script only sends me an email, to inform me the pi has restarted.
I've put the line "@reboot sudo python /home/pi/sendmail.py" in crontab, and this won't work... All of the command line is correct, because when I change the line to "* * * * * sudo python /home/pi/sendmail.py", it sends me the email every minute when the pi is on...
So I think there is something wrong with the @reboot command...
I'm doing the exact same thing: send me an email when you reboot. It does work, but when it doesn't, finding the cause can be challenging. Here's my crontab entry:

Code: Select all

@reboot	/usr/bin/python3 /home/pi/email_on_reboot.py > /home/pi/cronjoblog 2>&1
If there's an exception/error you'll get something that might be helpful in `/home/pi/cronjoblog`

Return to “Beginners”

Who is online

Users browsing this forum: jackandjill and 6 guests