Kannfastnix
Posts: 1
Joined: Thu Dec 20, 2018 3:13 pm

distinguish between booting and rebooting

Thu Dec 20, 2018 3:18 pm

Is there a way to distinguish between booting and rebooting? I have a script that i only want to run when rebooting.

Greetings

User avatar
mooblie
Posts: 122
Joined: Fri Oct 14, 2016 2:07 pm
Location: The Scottish Highlands

Re: distinguish between booting and rebooting

Thu Dec 20, 2018 4:03 pm

Write a unique string of text into some volatile storage (RAM) and then after a boot/reboot:

- if the string's still there, it was probably a warm soft reboot; but
- if the string's not there, it was probably a cold hard boot?

Any good?

epoch1970
Posts: 3064
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: distinguish between booting and rebooting

Thu Dec 20, 2018 5:52 pm

I don't see how a value in RAM can resist any sort of reboot??

You can prepare in advance:
- before reboot, save a flag file that says "rebooting", then reboot.
Save it to a non-volatile location. /tmp is volatile. Normally /var/tmp shouldn't be but do check. Or use /home or even /boot.
- at every boot, search for the flag, and erase it so it won't be found again at next boot. If the flag was found, run your task.

You can also try using the "last" command: "last -x reboot", "last -x shutdown", "last -x runlevel" (discussion on SE).
Personally I find the output of "last" confusing, and the absence of RTC on a Pi does not help.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

rhubarbdog
Posts: 64
Joined: Mon Dec 11, 2017 9:58 pm

Re: distinguish between booting and rebooting

Fri Dec 21, 2018 2:39 am

Look into runlevel.
Runlevel is set to 1 at initial boot. It increases to 4 when your operating system move ito multi user mode.
When you select reboot this is set to 6 it remains at 6 throughout the power down process

User avatar
DougieLawson
Posts: 35378
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: distinguish between booting and rebooting

Fri Dec 21, 2018 10:07 am

After the first boot from a fresh SDCard there's no difference between a boot and a reboot from an OS point of view. First boot of Raspbian does a whole bunch of stuff. First boot of NOOBS presents the "choose an OS to install" menu.

The main difference is if have you taken the power off and reconnected it (power on reset sets up the hardware peripherals from cold). That can't be detected by software which can't tell once it's booted and started long running services. You can force a power on reset with the microUSB still connected by connecting the _RESET_ pin to GND. Don't do that on a running system you could trash the filesystem on your SDCard or USB device.

So what are you trying to detect? Why is it important to you? On a clean shutdown you can find the time by reading the tail of /var/log/syslog you can see the boot with /var/log/kern.log and a dmesg command.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
DougieLawson
Posts: 35378
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: distinguish between booting and rebooting

Fri Dec 21, 2018 10:11 am

rhubarbdog wrote:
Fri Dec 21, 2018 2:39 am
Look into runlevel.
Runlevel is set to 1 at initial boot. It increases to 4 when your operating system move ito multi user mode.
When you select reboot this is set to 6 it remains at 6 throughout the power down process
Not with systemd it isn't, it disappered with the (uneventful) demise of sysvinit. Systemd got rid of all that runlevel junk in favour of "targets". There's no popular DebIan or Redhat based distro that still runs with sysvinit.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

n67
Posts: 939
Joined: Mon Oct 30, 2017 4:55 pm

Re: distinguish between booting and rebooting

Fri Dec 21, 2018 12:14 pm

As usual, no clarification from OP, so we are left to try to figure it out on our own.

My reading of the original thread was that he wants to distinguish between a cold boot (i.e., power on) and a warm boot (software initiated reboot). As has been noted, there's no real way to do this, from a software perspective and which is going to be 100% reliable (or close to 100%).

The obvious question, of course, is: Why do we want to know this? Only OP can clarify that.

That all said, I wonder if you get a heuristic of this by examining the "syslog" file(s) (or possibly other log files in /var/log) and seeing if there is a time gap before the last boot. The idea is that if the Pi has been off for a while, then gets booted, you might conclude that was a "cold boot", while if it was active (doing stuff - putting stuff in the syslog) just before being booted, then it was probably a "warm boot".

Not great, but it might be something to try...
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
thagrol
Posts: 1616
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: distinguish between booting and rebooting

Fri Dec 21, 2018 12:31 pm

I'd probably do it by aliasing the shutdown and reboot commands to a script that writes an empty file somewhere before calling the real shutdown or reboot command.

Then have a startup process to check for the presence of said file before deleting it. Delete it so that it only exist over one reboot.

Doesn't catch all cases but I'm not sure there is a way to catch 'em all.
This space unintentionally left blank.

hortimech
Posts: 265
Joined: Wed Apr 08, 2015 5:52 pm

Re: distinguish between booting and rebooting

Fri Dec 21, 2018 12:35 pm

DougieLawson wrote:
Fri Dec 21, 2018 10:11 am
There's no popular DebIan or Redhat based distro that still runs with sysvinit.
I suppose it depends on your definition of 'popular', but there is Devuan and on an rpi3 it is 64bit.

User avatar
DougieLawson
Posts: 35378
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: distinguish between booting and rebooting

Fri Dec 21, 2018 5:27 pm

devuan isn't worth the pain. Get used to systemd and embrace the way it improves the boot process.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

hortimech
Posts: 265
Joined: Wed Apr 08, 2015 5:52 pm

Re: distinguish between booting and rebooting

Fri Dec 21, 2018 5:51 pm

DougieLawson wrote:
Fri Dec 21, 2018 5:27 pm
devuan isn't worth the pain. Get used to systemd and embrace the way it improves the boot process.
It doesn't improve the boot process, it takes longer to boot!
systemd is like the curates egg, good in parts, pity the good bits are so small :cry:

User avatar
DougieLawson
Posts: 35378
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: distinguish between booting and rebooting

Sat Dec 22, 2018 9:50 am

hortimech wrote:
Fri Dec 21, 2018 5:51 pm
DougieLawson wrote:
Fri Dec 21, 2018 5:27 pm
devuan isn't worth the pain. Get used to systemd and embrace the way it improves the boot process.
It doesn't improve the boot process, it takes longer to boot!
systemd is like the curates egg, good in parts, pity the good bits are so small :cry:
That's not my experience. My headless raspberries take less than 20 seconds from power on to usable. The thing that slows things down is the GUI. Adding a new service with sysvinit is a pain. Adding that with systemd is trivial.

If you want to beat your head against the wall with devuan then please continue to do so.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

n67
Posts: 939
Joined: Mon Oct 30, 2017 4:55 pm

Yet another "Systemd: Good or bad?" debate

Sat Dec 22, 2018 12:16 pm

Can this thread be re-titled (globally) to be as shown above? Thanks.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
joan
Posts: 14017
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: distinguish between booting and rebooting

Sat Dec 22, 2018 12:16 pm

At boot (power-up) the GPIO are set to a particular state with their pulls. The pulls are not affected by a reboot. Choose a GPIO and change the pull to the opposite of the power-up setting. You can then determine the pull in software and thus distinguish between boot and reboot.

n67
Posts: 939
Joined: Mon Oct 30, 2017 4:55 pm

Re: distinguish between booting and rebooting

Sat Dec 22, 2018 12:18 pm

joan wrote:
Sat Dec 22, 2018 12:16 pm
At boot (power-up) the GPIO are set to a particular state with their pulls. The pulls are not affected by a reboot. Choose a GPIO and change the pull to the opposite of the power-up setting. You can then determine the pull in software and thus distinguish between boot and reboot.
Hey! That's actually a good idea. I'll have to test this at some point. Thanks!
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
joan
Posts: 14017
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: distinguish between booting and rebooting

Sat Dec 22, 2018 5:20 pm

The following appears to work (assumes the pigpio daemon is running).

Code: Select all

#!/bin/bash

pull=$(pigs r 27) # read GPIO 27, it defaults to pull-down on power-up.
pigs pud 27 u     # set pull-up on GPIO 27.
if ((pull == 0))
then
   echo "system was booted"
else
   echo "system was rebooted"
fi
N.B. The pull-up flag is set in the script so this will only return booted the first time the script is run after power-up. It might be desirable to move the pigs pud 27 u # set pull-up on GPIO 27. line to another script.

epoch1970
Posts: 3064
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: distinguish between booting and rebooting

Sat Dec 22, 2018 6:37 pm

joan wrote:
Sat Dec 22, 2018 5:20 pm
The following appears to work (assumes the pigpio daemon is running).
...
Brilliant! :idea: :idea: :idea:
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Return to “Beginners”