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

How to automatically restart the pi every day?

Thu Aug 13, 2015 3:24 pm

I have run into a problem while being away from home over the summer. My access to the home LAN is by way of VPN (OpenVPN and PPTP) both served by a Pi1B+. It is also the network CUPS printer server.

It has worked fine for almost the whole summer but three days ago I lost connection via VPN.
I have a Synology DS 212j NAS also on the network and I have set it up to be possible to reach from outside (for getting files and such private cloud things) and this connection is still working, so there is no Internet outage responsible for my problems.
What I have to do now is to ask my neighbors to use the key they have got and go into my study and power cycle the Pi units by pulling the power connector. Not so fun...

But I want to avoid this happening in the future so I would like to know if there is a way to make the Pi automatically restart at a certain time of day, say 04:00 every morning.

This would possibly keep the Pi in better shape and not bail out like it has now.
Bo Berglund
Sweden

scotty101
Posts: 4099
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to automatically restart the pi every day?

Thu Aug 13, 2015 3:54 pm

Code: Select all

sudo crontab -e
Add this to restart at midnight

Code: Select all

@midnight /sbin/shutdown -r now
or to restart at 4am

Code: Select all

0 4 * * * /sbin/shutdown -r now
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: How to automatically restart the pi every day?

Thu Aug 13, 2015 4:06 pm

You don't need 'sudo' privs to run the 'crontab' command. It is not one of those "special" commands that you always have to prefix with 'sudo'. Many people seem to think that it is safer to just prefix every command with 'sudo'.

Instead, just edit the normal 'pi' crontab, using the normal syntax, then in the crontab do:

Code: Select all

sudo /sbin/shutdown ...
(Just like you normally do from the command line!)
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
DougieLawson
Posts: 40138
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to automatically restart the pi every day?

Thu Aug 13, 2015 4:16 pm

Joe Schmoe wrote: Instead, just edit the normal 'pi' crontab, using the normal syntax, then in the crontab do:

Code: Select all

sudo /sbin/shutdown ...
(Just like you normally do from the command line!)
Strictly that should be

Code: Select all

59 23 * * * /usr/bin/sudo /sbin/shutdown ...
because you have to assume that cron has no environment settings.

I'd do it with an @reboot line in /etc/cron.d/reboot

Code: Select all

@reboot /sbin/shutdown -r 23:59
Or /etc/rc.local

Code: Select all

/sbin/shutdown -r 23:59 &
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: How to automatically restart the pi every day?

Thu Aug 13, 2015 4:48 pm

Strictly that should be
CODE: SELECT ALL
59 23 * * * /usr/bin/sudo /sbin/shutdown ...
because you have to assume that cron has no environment settings.
Not really. That's another one of those urban-myth-y things that refuses to die.

First of all, you are under no obligation to "assume" any such thing.

Second, there is a default environment, which includes a default PATH and that default PATH does include the essential system directories. Essentially, it includes everything you expect to have EXCEPT ~pi/bin.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
DougieLawson
Posts: 40138
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to automatically restart the pi every day?

Thu Aug 13, 2015 6:51 pm

I ran a cronjob which just called /usr/bin/env

Code: Select all

Date: Thu, 13 Aug 2015 19:41:01 +0100 (BST)
From: Cron Daemon <root@localhost>
To: pi@localhost
Subject: Cron <pi@saturn> /usr/bin/env

HOME=/home/pi2
LOGNAME=pi
PATH=/usr/bin:/bin
LANG=en_GB.UTF-8
SHELL=/bin/sh
PWD=/home/pi
/usr/sbin and /sbin are not in the default PATH, but /usr/bin is so sudo can be found without being fully qualified.
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

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

Re: How to automatically restart the pi every day?

Thu Aug 13, 2015 7:18 pm

59 23 * * * /usr/bin/sudo /sbin/shutdown ...
What does 59 23 * * * mean?
Bo Berglund
Sweden

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: How to automatically restart the pi every day?

Thu Aug 13, 2015 7:25 pm

It means one minute before midnight, every day, from now on.

Read it as executing on the 59th minute of the 23rd hour, and the rest are wildcards so it will do that every day.

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

Re: How to automatically restart the pi every day?

Thu Aug 13, 2015 7:28 pm

So they use a system with the least significant number first...
OK, I got it:

Code: Select all

sudo crontab -e
0 4 * * * /sbin/shutdown -r now
Will reboot at 4 AM every day
Bo Berglund
Sweden

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: How to automatically restart the pi every day?

Thu Aug 13, 2015 7:35 pm

Yep, 4am, you got it.

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: How to automatically restart the pi every day?

Thu Aug 13, 2015 11:45 pm

Joe Schmoe wrote:You don't need 'sudo' privs to run the 'crontab' command. It is not one of those "special" commands that you always have to prefix with 'sudo'. Many people seem to think that it is safer to just prefix every command with 'sudo'.

Instead, just edit the normal 'pi' crontab, using the normal syntax, then in the crontab do:

Code: Select all

sudo /sbin/shutdown ...
(Just like you normally do from the command line!)
As the 'pi' user can't issue the reboot command, there has to be a sudo in there somewhere. The example given edits the root crontab (hence no sudo in the entry), so sudo is needed (not to run crontab, but to edit the root crontab).

scotty101
Posts: 4099
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 8:57 am

asandford wrote: As the 'pi' user can't issue the reboot command, there has to be a sudo in there somewhere. The example given edits the root crontab (hence no sudo in the entry), so sudo is needed (not to run crontab, but to edit the root crontab).
Exactly why i did it that way in the first reply to the topic. Then Schmoe came along thinking he knew better.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

boyoh
Posts: 1526
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 9:25 am

Would it be possible to use a electronic timer
to switch a Opto isolator to give you the
pulse to re-start the Pi at the time you want.
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

User avatar
buja
Posts: 582
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 10:05 am

Why would you, cron is a software tool that is already there and does everything you need.
Hardware only makes things more complicated.

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

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 11:09 am

I would like to check if the restart works as planned.
Is there some way I can either log a timestamp for the reboot from the cron command or maybe there is some log already that can show this?
Bo Berglund
Sweden

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 12:06 pm

You could set the reboot time to the next hour from now and observe it.

scotty101
Posts: 4099
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 12:14 pm

Bosse_B wrote:I would like to check if the restart works as planned.
Is there some way I can either log a timestamp for the reboot from the cron command or maybe there is some log already that can show this?
Yes. The system records the last reboot time

Code: Select all

last reboot | head -1
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 12:24 pm

Or, if you really want to plunge into high tech rocket science, there is this command called "uptime", that tells you how long the system has been up. Or "w", which tells you the same and more.

Or, if you want to know when the system was booted, you can do this (from a script I wrote long ago that calculates the boot time and then does some other calculations based on that):

(This is GAWK code, but should also work with the standard MAWK that ships with Debian/Raspbian)

Code: Select all

BEGIN {
 getline < "/proc/uptime"
        print "The system was booted at:", strftime("%c",systime()-$1)
}
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

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

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 12:40 pm

What's wrong with the good old fashioned "who -b"?

Code: Select all

pi@raspi3 ~ $ who -b
         system boot  2015-08-03 08:02

User avatar
DougieLawson
Posts: 40138
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 12:53 pm

With Jessie you can use uptime -s

With Wheezy you can get the same answer with

Code: Select all

date --date=@`awk '/btime/ {print $2}' /proc/stat`
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

boyoh
Posts: 1526
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 1:03 pm

buja wrote:Why would you, cron is a software tool that is already there and does everything you need.
Hardware only makes things more complicated.

If you have a better Idea to the problem
tell him what to do, Some times hardware
is more reliable than software. If software
was the answer, it would have been solved
by now.
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

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

Re: How to automatically restart the pi every day?

Fri Aug 14, 2015 1:31 pm

rpdom wrote:What's wrong with the good old fashioned "who -b"?

Code: Select all

pi@raspi3 ~ $ who -b
         system boot  2015-08-03 08:02
Seemed like it did work:

Code: Select all

pi@raspbian-pi ~ $ who -b
         system boot  2015-08-14 05:00
Thanks for all your help!
This should make that pi a bit more stable. It has been known to hang before and then only a power cycle helped. It runs the PPTP VPN server, the OpenVPN server and a CUPS printer server too. Something makes it crash after a week or three.
Not a big problem when I am at home but like now 100 km away it's not easy to recover by the power cycle.
My neighbors got back home from vacation yesterday night and could do the power cycle so I got back on line again. :)
Bo Berglund
Sweden

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: How to automatically restart the pi every day?

Sat Aug 15, 2015 3:18 pm

Here's a another, reasonably reliable, way to find when the system was booted:

Code: Select all

$ ls -d --full-time /proc/1
dr-xr-xr-x 7 root root 0 2014-07-08 15:28:48.830000000 -0400 /proc/1
$
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: How to automatically restart the pi every day?

Sat Aug 15, 2015 7:40 pm

I just use "w" so I hardly ever make a typo.

Code: Select all


jimmy@ rpi: ~/scripts/projects
>$ w
 15:27:17 up 2 days, 23:22,  1 user,  load average: 0.02, 0.06, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
jimmy    pts/0    192.168.0.4      08:06    1.00s  0.55s  1.02s sshd: jimmy [priv]


Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: How to automatically restart the pi every day?

Sat Aug 15, 2015 8:02 pm

The thing is, 'w' (and uptime, which is a subset of 'w') gives you the uptime, not the boot time. In theory, you can calculate one from the other, but it is not quite the same thing.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Return to “Troubleshooting”