Last active
January 21, 2025 13:33
-
-
Save Guiorgy/e24cfd65b031ca09e0a8a3affa8b9e2b to your computer and use it in GitHub Desktop.
Calculate the total boot time in seconds on a Systemd system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Copyright © 2024 Guiorgy | |
# | |
# This program is free software: you can redistribute it and/or modify it under | |
# the terms of the GNU General Public License as published by the Free Software | |
# Foundation, either version 3 of the License, or (at your option) any later | |
# version. | |
# | |
# This program is distributed in the hope that it will be useful, but WITHOUT | |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
# FOR A PARTICULAR PURPOSE. | |
# | |
# You can see the full GNU General Public License at | |
# <https://www.gnu.org/licenses/> for more details. | |
# Extract the total boot time part after the '=' sign of 'systemd-analyze time' output | |
total_time=$(systemd-analyze time | head -n 1 | awk -F '= ' '{print $2}') | |
# Function to convert a single time interval ('dd.ddd[m|s|ms|us|ms]') to seconds | |
convert_interval_to_seconds() { | |
interval=$1 | |
if echo "$interval" | grep -qP '\dy$'; then | |
# Years | |
years=$(echo "$interval" | grep -oP '\d+(?=y)') | |
interval_seconds=$(echo "$years * 60 * 60 * 24 * 365" | bc) | |
elif echo "$interval" | grep -qP '\dmonth$'; then | |
# Months | |
months=$(echo "$interval" | grep -oP '\d+(?=month)') | |
interval_seconds=$(echo "$months * 60 * 60 * 24 * 30" | bc) | |
elif echo "$interval" | grep -qP '\dw$'; then | |
# Weeks | |
weeks=$(echo "$interval" | grep -oP '\d+(?=w)') | |
interval_seconds=$(echo "$weeks * 60 * 60 * 24 * 7" | bc) | |
elif echo "$interval" | grep -qP '\dd$'; then | |
# Days | |
days=$(echo "$interval" | grep -oP '\d+(?=d)') | |
interval_seconds=$(echo "$days * 60 * 60 * 24" | bc) | |
elif echo "$interval" | grep -qP '\dh$'; then | |
# Hours | |
hours=$(echo "$interval" | grep -oP '\d+(?=h)') | |
interval_seconds=$(echo "$hours * 60 * 60" | bc) | |
elif echo "$interval" | grep -qP '\dmin$'; then | |
# Minutes | |
minutes=$(echo "$interval" | grep -oP '\d+(?=min)') | |
interval_seconds=$(echo "$minutes * 60" | bc) | |
elif echo "$interval" | grep -qP '\ds$'; then | |
# Seconds | |
seconds=$(echo "$interval" | grep -oP '\d+\.\d+|\d+(?=s)') | |
interval_seconds=$seconds | |
elif echo "$interval" | grep -qP '\dms$'; then | |
# Milliseconds | |
milliseconds=$(echo "$interval" | grep -oP '\d+\.\d+|\d+(?=ms)') | |
interval_seconds=$(echo "$milliseconds / 1000" | bc -l) | |
elif echo "$interval" | grep -qP '\dus$'; then | |
# Microseconds | |
microseconds=$(echo "$interval" | grep -oP '\d+\.\d+|\d+(?=us)') | |
interval_seconds=$(echo "$microseconds / 1000000" | bc -l) | |
elif echo "$interval" | grep -qP '\dns$'; then | |
# Nanoseconds | |
nanoseconds=$(echo "$interval" | grep -oP '\d+\.\d+|\d+(?=ns)') | |
interval_seconds=$(echo "$nanoseconds / 1000000000" | bc -l) | |
else | |
# WTF? | |
interval_seconds=$interval | |
fi | |
echo "$interval_seconds" | |
} | |
# Initialize total seconds | |
total_seconds=0 | |
# Split the total_time into intervals and sum them | |
for interval in $total_time; do | |
interval_seconds=$(convert_interval_to_seconds "$interval") | |
total_seconds=$(echo "$total_seconds + $interval_seconds" | bc) | |
done | |
# Print the total time in seconds without trailing zeroes | |
echo "$total_seconds" | awk '{printf "%g\n", $0}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment