Distinguish between reboot and halt/poweroff

Hi all,
I need to detect whether the device is rebooting or halting/poweroff in the shutdown section of an init.d script.
In other systems I use the output of the "runlevel" or "systemctl" commands.
I successfully added the busybox runlevel command to my image, but its output is always "Unknown"; I understand from the docs that the runlevel concept itself is not implemented.
Is there a way to accomplish what I want? If not possible in a script I'm also interested to implement it in a C program, using for example the libubus or libubox API.
Thank you.

I think that this question had come up in the forums before, although I don't remember the context (it could have been user/script reboot vs crash-based reboot vs power cut).

IIRC, the proposed solution was to modify the reboot behavior. This could be done by recompiling busybox after editing the reboot routine, or by making a script that is used to call the reboot. In both cases, I believe the idea was to leave a breadcrumb (for example, touch a file or a variable within a file somewhere). Then, in an init.d script, detect the condition and reset that operation (reset the variable or delete the file) upon the next bootup.

Note that this is not ideal for systems with flash storage (especially cheaper embedded systems) since it may cause additional wear. But if the reboots are relatively infrequent (especially at human time scales), that would probably not be a significant problem.

I ended up replacing the reboot command (that by default is a link to busybox) with a custom script that simply writes a file in the /tmp folder and then calls "busybox reboot".
It seems to work.
Thank you very much for your hint.

/tmp (on most platforms, maybe all… not sure about x86) is a RAM disk. It will not persist across reboots.

It doesn't matter, I need to detect the reboot on system shutdown, not at the following boot.
It is rather better to preserve the flash as you said.
Thank you.

Ok... perfect!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.