Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script to restart a Raspberry Pi

Tags:

raspberry-pi

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.

like image 805
bighead85 Avatar asked Feb 15 '23 09:02

bighead85


1 Answers

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.

like image 91
stmfunk Avatar answered Feb 27 '23 13:02

stmfunk