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.
Ubuntu – How to get real uptime
uptime
uptime
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.
Best Answer
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)