Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: How to automatically restart the pi every day?

Sat Aug 15, 2015 9:57 pm

I'm completely satisfied with who -b to check the reboot.
It has worked every time I checked after installing the cron script item.
So my Pi is now rebooted once every 24 h and should therefore keep going without crashing, hopefully.
Bo Berglund
Sweden

DarrenHill
Posts: 265
Joined: Fri Oct 03, 2014 3:03 pm

Re: How to automatically restart the pi every day?

Tue Aug 18, 2015 7:23 pm

For the original requirement, I just came across this tutorial -

http://trendblog.net/raspberry-pi-basic ... ut-cables/

It talks about an app called Watchdog (item 6 in the pages list), which might be of interest to the OP as an alternative to periodic reboots perhaps? I'm just setting up a VPN server Pi myself, and am going to look at adding this in.

Does anyone know this app in more detail to comment on if it is useful or not?

fruit-uk
Posts: 609
Joined: Wed Aug 06, 2014 4:19 pm
Location: Suffolk, UK

Re: How to automatically restart the pi every day?

Tue Aug 18, 2015 8:16 pm

Yes, very useful. I have it on a Pi that was running fhem and had occasional issues, though none in months now.

I also have it on a Cubietruck (sort of similar to a Pi) with slightly a different config ie. it doesn't start monitoring until 15 mins after boot. Ths allows plenty of time to save a file from the equivalent of a cronjob and then repeatedly saves at intervals and is used for monitoring by watchdog.

One could even force a restart by deleting a monitored file at the approprate time.

Be careful when setting up and test well before starting from init.d or you may getinto a reboot loop :)

Aydan
Posts: 735
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: How to automatically restart the pi every day?

Wed Aug 19, 2015 10:19 am

Bosse_B wrote:This should make that pi a bit more stable. It has been known to hang before and then only a power cycle helped.
For this cas you could activate the watchdog. It will reset the Pi when the cpu hangs and doesn't periodically tell the watchdog it's still alive.

Regards
Aydan

PS: The watchdog is basically just a hardware timer in the SoC which resets the SoC when it runs out. Once activated (per powercycle) you have to periodically reset the counter or the SoC resets.

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: How to automatically restart the pi every day?

Fri Aug 21, 2015 7:28 am

Yes, I can appreciate that a watchdog would activate a reboot only when actually needed.
But when I read up on it I realized that there were gotchas also in that solution, which actually could put the pi into an endless loop of reboots.
It feels less risky to use the cron solution.
Additionally I have now put a triple redundant solution in place:
1) The original Pi2B with the reboot cron job serves the normal port
2) Another Pi1B, which runs a clone of the OpenVPN server but on a different port
3) My Synology DS212j had an OpenVPN function too so I run that on yet another port
All of these are port forwarded from my router.
So if any of them goes down I can use another to get on to the internal LAN.

Works as long as the internet is running, which it unfortunately is not at the moment.
My fiber provider back home had a service disruption on Wednesday afternoon and it has not been solved yet!
For such things I do not have a solution. :(
:evil:
Bo Berglund
Sweden

fruit-uk
Posts: 609
Joined: Wed Aug 06, 2014 4:19 pm
Location: Suffolk, UK

Re: How to automatically restart the pi every day?

Fri Aug 21, 2015 7:39 am

Reboot loops with watchdog are easy to do ;)

From memory the using net link options at the start of the config file would do this - watchdog would reboot before the links were available.
This is why a delayed start is a good idea, the machine will be fully up and running and settings can be tweaked with no fear of an instant reboot.

At the end of the day do whatever works for you

Aydan
Posts: 735
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: How to automatically restart the pi every day?

Fri Aug 21, 2015 9:08 am

In default configuration the watchdog doesn't care about links or anything other. Actually the watchdog doen't know about anything going on on the CPU. But again, the default config of the Watchdog daemon will check nothing, only reset the watchdog when it gets scheduled by the kernel.
So as long as the kernel runs, watchdog will not kick in. Cron can only work as long as the system is still alive. If the system is dead, watchdog will kick in. That's why I suggested the watchdog solution.
I don't know what gave you the idea of the watchdog actually caring about links. You can of course make it care about links, but as was already said, if done ncorrectly this can lead to reboot loops.

Regards
Aydan

fruit-uk
Posts: 609
Joined: Wed Aug 06, 2014 4:19 pm
Location: Suffolk, UK

Re: How to automatically restart the pi every day?

Fri Aug 21, 2015 9:19 am

Well, of course - I was assuming a user may configure the links or other options, not expect defaults to work out of the box.

ISTR a number of posts about enabling links at the start and reboot loops in the past though that may no longer be the case. It certainly caught me out a long time ago checking against my router.

watchdog is a great solution - and I use it on several machines but for the unwary it needs a little care when setting up

Aydan
Posts: 735
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: How to automatically restart the pi every day?

Fri Aug 21, 2015 9:25 am

I'm quoting myself here to make clear that I intended the watchdog only for system hangs, not if the link was up. And for that you don't need to configure anything because that is the default behaviour. For the rest you can use cron scripts which is indeed easier to handle.
Aydan wrote:
Bosse_B wrote:This should make that pi a bit more stable. It has been known to hang before and then only a power cycle helped.
For this cas you could activate the watchdog. It will reset the Pi when the cpu hangs and doesn't periodically tell the watchdog it's still alive.

Regards
Aydan

PS: The watchdog is basically just a hardware timer in the SoC which resets the SoC when it runs out. Once activated (per powercycle) you have to periodically reset the counter or the SoC resets.

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: How to automatically restart the pi every day?

Fri Aug 21, 2015 5:50 pm

When I get back home and have physical control I will have a look at the watchdog solution. I could set it up on one of the Pi instead of the daily reset and then I can evaluate which solution is the better one.
Is there some way to log the startup time into a logfile of my own?
Like an autoexec.bat file as in MS-DOS where I can enter some logging command?
Bo Berglund
Sweden

Aydan
Posts: 735
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: How to automatically restart the pi every day?

Fri Aug 21, 2015 5:59 pm

Bosse_B wrote:Is there some way to log the startup time into a logfile of my own?
Like an autoexec.bat file as in MS-DOS where I can enter some logging command?
No need for that. Install uprecords and you're set, although uprecord is known to be forgetful on hard resets. I've lost the records file a few times on one of my raspberries. Or you can add something to /etc/rc.local or add a cronjob with @reboot to log into a file.

Regards
Aydan

guruabyss
Posts: 46
Joined: Wed Sep 25, 2019 7:54 pm

Re: How to automatically restart the pi every day?

Mon Sep 30, 2019 3:43 am

If you have auto update on and it starts updating at 11:59, you have @midnight /sbin/shutdown -r now. Will it reboot even if its updating?

brandtz
Posts: 6
Joined: Fri Oct 28, 2016 2:13 pm

Re: How to automatically restart the pi every day?

Wed Aug 12, 2020 11:03 am

Hi,

I did a cron job yesterday to reboot every night, Now trying to see if it has rebooted I get a list of 20-30 "reboots" with same time: "" the "who -b" says "system boot Jan 1 01:00"...feels this is some clock issue. The command "last reboot" gives the list will all the same: "reboot system boot 4.19.75+v7+ Thu Jan 1 01:00 ...."

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: How to automatically restart the pi every day?

Wed Aug 12, 2020 2:07 pm

Even though this is a REALLY old thread from 2015 I will chip in with what I have done.
I am using cron to reboot the RPi once every night.
In /etc/rc.local I have a call to a script I have written to report the reboot.
This script starts execution with a sleep for a minute, then it checks for Internet access and if none present it goes back to the sleep again.
All until the Internet is available, when it sends a message to my webserver which then logs the reboot and sends me an email about the reboot.
This is pretty straightforward stuff, really...
It has worked fine on all of the dozen or so RPi units I have installed it on.
Bo Berglund
Sweden

brandtz
Posts: 6
Joined: Fri Oct 28, 2016 2:13 pm

Re: How to automatically restart the pi every day?

Wed Aug 12, 2020 2:37 pm

OK, thanks! Could you forward the script and the details in the call to it?

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: How to automatically restart the pi every day?

Wed Aug 12, 2020 3:08 pm

Here is the script I install on all of my RPi units in the /home/pi/bin dir.
It is edited to take out identification details for my own webserver...

Code: Select all

nano ~/bin/ipreport.sh
#Enter this into the file:
#!/bin/bash

#Wait for a minute if no argument supplied, otherwise continue:
URL="google.com"
#URL="url to your own webserver where the mail reporter resides"

#Check for initial delay on command line
if [ -z "$1" ]; then
  DELAY="60"
else
  DELAY="$1"
fi
sleep $DELAY

# Test connectivity to Internet at $DELAY intervals, do not continue until Internet is available
ONLINE=1
while [ $ONLINE -ne 0 ]
do
   ping -q -c 1 -w 1 $URL >/dev/null 2>&1
   ONLINE=$?
   if [ $ONLINE -ne 0 ]
     then
       echo "Failed test to $URL so wait a while and repeat!"
       sleep $DELAY
   fi
done

#Here only if we have an Internet connection
#Now we need to find the external IP address.
#It is reported using http://checkip.dyndns.com/
ExtIPaddr="$(wget -q -O - "http://checkip.dyndns.com/" | sed "s/^.*: \([0-9.]*\)<.*$/\1/")"

#Log live activity on bootup
CurrDate=$(date "+%Y%m%d")
CurrTime=$(date "+%H:%M:%S")
echo "---------------------------------------" >> "/home/pi/log/uptime.log"
echo "IPReport at $CurrDate $CurrTime $ExtIPaddr" >> "/home/pi/log/uptime.log"

#Grab internal network data for the email report
RPIname=$(hostname)
IPAddr=$(ifconfig eth0 | grep 'inet ' | sed 's/addr://' | awk '{ print $2 }')
IPAddrWiFi=$(ifconfig wlan0 | grep 'inet ' | sed 's/addr://' | awk '{ print $2 }')
IPMac=$(cat /sys/class/net/eth0/address)
IPMacWiFi=$(cat /sys/class/net/wlan0/address)

echo "Hostname:    $RPIname"
echo "External IP: $ExtIPaddr"
echo "Eth IP addr: $IPAddr"
echo "Eth  MAC:    $IPMac"
echo "WiFi addr:   $IPAddrWiFi"
echo "WiFi MAC:    $IPMacWiFi"


#Send request to webserver to report bootup, only if using your own webserver
#with the php script ipreport.php, which posts an email to yourself
#curl --data "rpiaddr=$IPAddr" --data "rpiname=$RPIname" --data "rpiaddrwifi=$IPAddrWiFi" --data "rpimacaddr=$IPMac" --data "rpimacaddrwifi=$IPMacWiFi" "http://$URL/php/ipreport.php"
exit 0
The call to the script above is set in /etc/rc.local:

Code: Select all

sudo nano /etc/rc.local
#Add this to the end (before exit0):
#Send IP address report after a start delay of 60 seconds
sleep 60
/home/pi/ipreport.sh &
The reboot is scheduled via cron:

Code: Select all

sudo crontab -e
# Reboot at 01:15 every night to ensure operations
15 1 * * * /sbin/reboot
Bo Berglund
Sweden

brandtz
Posts: 6
Joined: Fri Oct 28, 2016 2:13 pm

Re: How to automatically restart the pi every day?

Thu Aug 13, 2020 11:28 am

Wow, thats adavnced! and unfortunately over my competence to sort out. I will try again to do "15 1 * * * /sbin/reboot" since the former cron job does not reboot.

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: How to automatically restart the pi every day?

Thu Aug 13, 2020 12:39 pm

brandtz wrote:
Thu Aug 13, 2020 11:28 am
Wow, thats adavnced! and unfortunately over my competence to sort out. I will try again to do "15 1 * * * /sbin/reboot" since the former cron job does not reboot.
The complexity of the script is because it is designed to REPORT the reboot to a logfile and to an email.
But the reboot itself is handled by cron.
Remember to use the admin version of cron:

Code: Select all

sudo crontab -e 
when you set it up because you need sudo privileges to actually reboot the machine.
Bo Berglund
Sweden

brandtz
Posts: 6
Joined: Fri Oct 28, 2016 2:13 pm

Re: How to automatically restart the pi every day?

Thu Aug 13, 2020 12:45 pm

Ys, did that
"pi@KontorRaspberry:~ $ sudo crontab -e
crontab: installing new crontab"

Will see if it works...

pfletch101
Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: How to automatically restart the pi every day?

Fri Aug 14, 2020 8:15 pm

brandtz wrote:
Wed Aug 12, 2020 11:03 am
Hi,

I did a cron job yesterday to reboot every night, Now trying to see if it has rebooted I get a list of 20-30 "reboots" with same time: "" the "who -b" says "system boot Jan 1 01:00"...feels this is some clock issue. The command "last reboot" gives the list will all the same: "reboot system boot 4.19.75+v7+ Thu Jan 1 01:00 ...."
You have been given some good ways of doing what you want to do. I suspect that what was happening was that fake-hwclock was mostly saving and restoring the reboot time, so cron immediately rebooted again and again until their was enough 'jitter' in the time to convince it that reboot time had passed.

User avatar
Roken
Posts: 383
Joined: Sun Dec 31, 2017 4:35 pm
Location: Wales, home of the Pi

Re: How to automatically restart the pi every day?

Sat Aug 15, 2020 7:24 am

Bosse_B wrote:
Sat Aug 15, 2015 9:57 pm
I'm completely satisfied with who -b to check the reboot.
or just

Code: Select all

uptime
Headless PI. OMG, someone cut it's head off. Oh, hang on. it didn't have one to start with.

Return to “Troubleshooting”