Is there any easy way to calculate uptime excluding period system is suspended? I would like to count time I've spent in front of computer.
Is there any easy way to calculate uptime excluding period system is suspended? I would like to count time I've spent in front of computer.
On one hand this answer is six years late, on the other hand it's a blink of the eye if the internet is eternal!
You can get the real uptime with this little bash script:
Linux will report uptime as 8 days and 40 minutes. The real uptime (after subtracting suspend time) is about 2 days and 18 hours.
suspendtime
bash scriptHere's the code you can copy to your system:
About half the program is converting seconds to human readable format in weeks, days, hours, minutes and seconds. The function
DaysMinutes
does this and was copied from my~/.bashrc
file where you may want to put that function yourself.How it works
The key component is getting suspend start and end times from
journalctl
:journalctl -b-0
command reads all the system messages for the current boot. You could enhance the function to look at the previous boot using-b-1
the boot before that with-b-2
, etc.grep
command with regex does the heavy lifting returning 32 system messages pertaining to suspend from the 19,330 system messages recorded (on my system)After years the above script requires an update. systemd messages and date command are not compatible anymore - as explained in the comments to the upper script. So I take that one as a base and modified it. Journal returns date as "Mai 03 ..." which can't be handled by date. To work around I faked the year and month - so it will not count properly if month is changed during standby .... Maybe someone will change that.