[SOLVED] Reboot history

Hi everyone,

I'd like to "exactly" know when my device reboot by recording when it occurs.

To do that I placed this simple line in System --> Startup --> Local Startup:

echo -e "*** Device restarted on***" $(date +"%F_%H:%M:%S\n") >> /pr_home/Device_restart_history.txt

Of course /pr_home/Device_restart_history.txt is written on an USB stick (Extroot) in order to keep it among reboots; it works, but not as I expected about timestamps:

Init --> System log.

-rw-r--r-- 1 root root 52 Dec 24 17:47 Device_restart_history.txt ---> The file.

*** Device restarted on*** 2023-12-24_17:47:31 --> Its content.

Is it possible to record the reboot just after the init complete event?
In other words, how can I have my file and its content timestamps on Fri Dec 29 instead of Sun Dec 24?

Thanks a lot in advance.

You probably need to wait for a successful ntp sync. The problem is that most of these devices lack a real time clock, so the initial time is set to the latest timestamp of files written to the storage.

Consider adapting a similar solution for your situation by waiting to write that log entry until ntp sync is complete.

5 Likes

Thank you so much, I'll try it.

I confirm the following /etc/hotplug.d/ntp/90-Device_restart_history.sh does what I want:

#!/bin/sh
[ $ACTION = “stratum” ] && echo -e "*** Device restarted on" $(date +"%F_%H:%M:%S") " ***\n" >> /pr_home/Device_restart_history.txt

Thanks again and... happy 2024 :grinning:

Edited: not yet solved because the script does not run on reboot only....

*** Device restarted on 2024-01-02_16:26:05  ***  <-- manual reboot (test)

*** Device restarted on 2024-01-02_20:58:05  ***  <-- power issue reboot

*** Device restarted on 2024-01-02_21:53:13  ***  <-- power issue reboot

*** Device restarted on 2024-01-02_22:48:01  ***  <-- no reboot

*** Device restarted on 2024-01-02_22:48:34  ***  <-- no reboot

*** Device restarted on 2024-01-02_23:09:41  ***  <-- no reboot
...

You could install ntpdate and insert this above the command writing to the log file.

/usr/sbin/ntpdate 0.openwrt.pool.ntp.org

You need a semaphore to avoid false records.

[ $ACTION = "stratum" -a ! -f /tmp/log_updated ] && {
	echo -e "*** Device restarted on" $(date +"%F_%H:%M:%S") " ***\n" >> /pr_home/Device_restart_history.txt
	touch /tmp/log_updated
	exit 0
	}
3 Likes

Stratum action happens quite often, always when the reliability level of the ntp server used changes. (Stratum means how many intermediate NTP servers before an official atomic clock at stratum 0.)

That may happen quite often if you have a server pool, like the ntp aliases that we use.

2 Likes

Thanks for the hint!

But... is the following an equivalent way to reach the goal without installing ntpdate?

/etc/hotplug.d/ntp/90-Device_restart_history.sh:

#!/bin/sh
if [[ ! -e /tmp/Device_already_rebooted ]]; then
[ $ACTION = "stratum" ] && echo -e "*** Device restarted on" $(date +"%F_%H:%M:%S") " ***\n" >> /pr_home/Device_restart_history.txt
touch /tmp/Device_already_rebooted
fi

Yes, except you won't have to wait for the automatic sync to happen.
This should also work without installing any additional packages.

/usr/sbin/ntpd -p 0.openwrt.pool.ntp.org -q
1 Like

Unfortunately, my script doesn't work and (maybe) I'll investigate in the future... :confused:

Fortunately, it runs if I apply your hints as follows:

#!/bin/sh

/usr/sbin/ntpd -p 0.openwrt.pool.ntp.org -q

[ $ACTION = "stratum" -a ! -f /tmp/Device_already_rebooted ] && {
        echo -e "*** Device restarted on" $(date +"%F_%H:%M:%S") " ***\n" >> /pr_home/Device_restart_history.txt
        touch /tmp/Device_already_rebooted
        exit 0
        }

Thanks for your support :grinning:

1 Like

Just to clarify that this is only needed if you want to make your original approach work
(using /etc/rc.local).

# /etc/rc.local

/usr/sbin/ntpd -p 0.openwrt.pool.ntp.org -q
echo -e "*** Device restarted on" $(date +"%F_%H:%M:%S") " ***\n" >> /pr_home/Device_restart_history.txt

exit 0

Remove it from the hotplug script.

1 Like

You might simplify things by using "step" ntp hotplug action instead of "stratum". It is triggered when the NTP daemon originally gets a time from internet.

Reference to:

I use this kind of logging script:

root@router4:~# cat /etc/hotplug.d/ntp/20-ntpd-logger
#!/bin/sh
[ $ACTION = "step" ]    && logger -t ntpd Time set, stratum=$stratum interval=$poll_interval offset=$offset
[ $ACTION = "stratum" ] && logger -t ntpd Stratum change, stratum=$stratum interval=$poll_interval offset=$offset

Output in log:

root@router5:~# logread | grep ntpd
Fri Jan  5 12:46:10 2024 user.notice ntpd: Time set, stratum=16 interval=32 offset=45.140799
Fri Jan  5 12:46:14 2024 user.notice ntpd: Stratum change, stratum=2 interval=2 offset=0.000694

Interesting... so, if I correctly understand your hint, to reach my goal I could use instead a very simple /etc/hotplug.d/ntp/20-Device_restart_history.sh containing:

[ $ACTION = "step" ] && echo -e "*** Device restarted on" $(date +"%F_%H:%M:%S") " ***\n" >> /pr_home/20-Device_restart_history.txt

Am I right?

Yes, you understood me correctly.
I think that it should be enough for you.

Just try it and see if it works for you.

1 Like

I'll try it as well, many thanks.

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