4

I think what I want is straight forward.

Python script to restart my Raspberry Pi after 23 hours and 59 minutes. The reason I am trying to do this, instead of set times with a cron job, is the Pi has no onboard battery for a clock so I don't care what the time is (if connected to internet, it will source current time), just a count down of 23 hours and 59 minutes from the script starting.

This is as far as I have got;

def restart():
SendEmail = SendEmail "SYSTEM RESTART", "ncam.py auto restart initiated"      msg['Subject'], body)
command = "/usr/bin/sudo /sbin/shutdown -r now"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]

Also I want to send an email to myself with the set parameters as above.

2
  • Just a thought why not create a cron job (or the alike) and execute a python command that will restart your Pi? Otherwise (like below) you have to have a python keep looping and I'm not sure how much cpu/memory it would take up just to run that script. Commented Sep 2, 2013 at 6:50
  • 2
    why not use a shell script with a sleep $nearly_a_day; reboot;? Commented Sep 3, 2013 at 10:54

3 Answers 3

5

You are going to want some variant of this:

 import time
 import os
 currentTime = time.time()
 tomorrow = currentTime + 86340000
 while time.time() < tomorrow:
     do.yourCode()
    
 os.system("poweroff")

Put something like that in your function and it will do what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

while time.time() < tomorrow. See rtv's answer
3

You can simply restart raspberry with 'sudo reboot' command. Just put this command inside a python code and run it as system command. For example this code count down from 1 to 10 before restart:

import time
import os
for i in range(1,10):
       print 'hello',i
       #Do your code here
       time.sleep(1)
os.system("sudo reboot")

Use this method to countdown whatever time and restart pi.

Comments

2

You should probably change to

while time.time() < tomorrow

to avoid any potential "miss" of the exact millisecond to match.

1 Comment

Whilst you are correct (and I must admit that I thought the same), this looks like it is a comment to stmfunk's answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.