I am trying to calculate time taken by reboot on SUSE linux systems (SLES11 and SLES12 systems) as follows:
reboot_time = end_time - start_time
where
start_time is "time at which reboot command is triggered"
end_time is "time at which system is ready after booting process, finishing up startup and userspace programs" OR "time when login is prompted soon after reboot"
I am able to know start_time
. But not able to know time end_time
for SLES11 systems (init.d SysV version). For SLES12 (systemd initialization), systemd-analyze
was giving required information, but I am not able to figure out a reliable way for init.d systems. Is there any similar alternative on a SysV init system or SLES11 that could give me the time spent on booting (starting kernel, finish running startup programs and complete userspace initialization)?
You might get (non second precise) last shutdown date with the following command
last -x | egrep "shutdown|reboot" | head -1 | awk '{print $5" "$7" "$6" "$8}'
last -x
can give you system boot / reboot & shutdown times, and all system boot logs since system creation. The problem is that is does not show years, and has only minute resolution.
last
reads various log binary log files (/var/log/wtmp utmp and btmp)
You can use utmpdump
utility to get shutdown & reboot timestamps with more precise timestamps
utmpdump /var/log/wtmp | egrep "shutdown|reboot" | awk -F '[' '{print $9}' | sed 's/ ]//g'
Ultimatively, you can get the elapsed time from shutdown to reboot with the following (horribly long) one liner:
echo "System $(hostname) took $(($(date -d "$(LC_ALL=C utmpdump /var/log/wtmp 2>/dev/null| grep "reboot" | grep -Eo '[[:alpha:]]{3} [[:alpha:]]{3} [[:digit:]]{2} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2} [[:digit:]]{4}|[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}T[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}' | tail -1)" "+%s") - $(date -d "$(LC_ALL=C utmpdump /var/log/wtmp 2>/dev/null| grep "shutdown" | grep -Eo '[[:alpha:]]{3} [[:alpha:]]{3} [[:digit:]]{2} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2} [[:digit:]]{4}|[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}T[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}' | tail -1)" "+%s"))) to reboot"
Explanation:
LC_ALL=C
makes the output of the following commands unlocalizedutmpdump /var/log/wtmp 2>/dev/null
will list all events, while 2>/dev/null
will redirect errors and unwanted output.grep "reboot"
or grep "shutdown"
will filter utmpdump output to show only reboot or shutdown events'[[:alpha:]]{3} [[:alpha:]]{3} [[:digit:]]{2} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2} [[:digit:]]{4}'
is a regex to capture non localized date strings that look like "Fri Aug 02 00:01:27 2019", these are used on CentOS'[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}T[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}'
is a regex to capture ISO 8601 date strings that look like "2021-08-17T10:11:03", these are used on Debiandate -d "datestring"
converts the date into an epoch$(command)
is the output of the command$((x - y))
is for mathematical operations (eg substract two epochs)You can also play with the output of utmpdump
to retrieve login timestamps.
Be aware that shutdown/reboot cycle is different from shutdown/system boot cycle, hence the commands have to be adapted to your usage.
Also be aware that any forceful system resets will not be included in the logs, hence giving bogus values.