raspberrypifreak
Posts: 14
Joined: Sat Aug 03, 2019 4:30 am

how to detect if pi has rebooted?

Mon Sep 02, 2019 10:30 am

Hi all.

I have a pi zero running a python script. I am in an area where power is not consistent and blackouts occur regularly. When the power is cut and restored, the pi zero reboots, but the python script is not longer running. How can I automate the detection of when the pi reboots and then start the script again? Is there a log that is created when the pi reboots that can be checked by a bash script?

Thanks in advance.

User avatar
graysky
Posts: 98
Joined: Fri Apr 05, 2013 11:43 am
Location: /run/user/1000
Contact: Website

Re: how to detect if pi has rebooted?

Mon Sep 02, 2019 10:47 am

How are you triggering the script to run? Why not use cron to run @reboot and at some regular interval? What distro are you using? Raspbian? Likely you're using systemd as your init system so google journalctl. You could also optionally make a systemd timer and unit to run your script to avoid pulling down more dependencies (cron).

jj_0
Posts: 113
Joined: Wed Jul 11, 2012 7:07 am

Re: how to detect if pi has rebooted?

Mon Sep 02, 2019 10:50 am

Or run the script as a systemd service at startup.

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: how to detect if pi has rebooted?

Mon Sep 02, 2019 10:59 am

You can also get a battery hat, or buy a UPS to power a zero for days if need be.

User avatar
graysky
Posts: 98
Joined: Fri Apr 05, 2013 11:43 am
Location: /run/user/1000
Contact: Website

Re: how to detect if pi has rebooted?

Mon Sep 02, 2019 11:01 am

jj_0 wrote:
Mon Sep 02, 2019 10:50 am
Or run the script as a systemd service at startup.
graysky wrote:
Mon Sep 02, 2019 10:47 am
You could also optionally make a systemd timer and unit to run your script to avoid pulling down more dependencies (cron).
There's an echo in here :D

jj_0
Posts: 113
Joined: Wed Jul 11, 2012 7:07 am

Re: how to detect if pi has rebooted?

Mon Sep 02, 2019 11:08 am

graysky wrote:
Mon Sep 02, 2019 11:01 am
jj_0 wrote:
Mon Sep 02, 2019 10:50 am
Or run the script as a systemd service at startup.
graysky wrote:
Mon Sep 02, 2019 10:47 am
You could also optionally make a systemd timer and unit to run your script to avoid pulling down more dependencies (cron).
There's an echo in here :D
Great minds... :lol:

raspberrypifreak
Posts: 14
Joined: Sat Aug 03, 2019 4:30 am

Re: how to detect if pi has rebooted?

Mon Sep 02, 2019 11:15 am

graysky wrote:
Mon Sep 02, 2019 10:47 am
How are you triggering the script to run? Why not use cron to run @reboot and at some regular interval? What distro are you using? Raspbian?
I've been manually starting the script. I also manually check if the script is running - if it is not, then that is an indication that the pi has rebooted - and I manually start the script again. I am using raspbian buster lite. Can cron detect that a reboot has happened? Systemd sounds interesting.

raspberrypifreak
Posts: 14
Joined: Sat Aug 03, 2019 4:30 am

Re: how to detect if pi has rebooted?

Mon Sep 02, 2019 11:21 am

Thanks for all the replies. systemd sounds like something that can work in my situation.

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: how to detect if pi has rebooted?

Mon Sep 02, 2019 1:41 pm

"uptime -s" will tell you when it last rebooted...

Code: Select all

petero@EliteDesktop1:~$ uptime -s
2019-09-02 06:41:33
petero@EliteDesktop1:~$ 
see "man uptime"

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
graysky
Posts: 98
Joined: Fri Apr 05, 2013 11:43 am
Location: /run/user/1000
Contact: Website

Re: how to detect if pi has rebooted?

Mon Sep 02, 2019 2:12 pm

raspberrypifreak wrote:
Mon Sep 02, 2019 11:15 am
Can cron detect that a reboot has happened? Systemd sounds interesting.
Either method can run your script at boot and optionally at some regular interval... there is no need to detect a reboot in this context. Google is your friend or the Arch Wiki.

https://wiki.archlinux.org/index.php/Cron
https://wiki.archlinux.org/index.php/Systemd

User avatar
cormack
Posts: 44
Joined: Fri Jul 12, 2019 7:39 pm

Re: how to detect if pi has rebooted?

Sun May 03, 2020 10:28 pm

Old thread - sorry for reviving, but wanted to note I can think of a number of reasons why a process can stop running, other than a system reboot. Code could be buggy and crash, someone can "kill" the process, and so on. Regardless, I too recommend using systemd. One of it's strengths over the older SysVinit system was it's ability to detect that a process has shut down, and restart it.

As for how to detect if your just using a cron task, you could have cron run a script that does this.
Assuming your process is called "myprogram"...

#!/bin/bash
# Look to see if the process is already running
if [ -n "$(ps -ef | grep [m]yprocess)" ]
then
# If not running, launch the process.
# Replace the next line with whatever command is needed to start your process
nohup /path/to/myprocess &
fi
# Otherwise, do nothing, and exit

Return to “Beginners”