The following commands work with Python 2.7.15rc1 on my Mint 19.
They will display the uptime excluding sleep time.
python3 -c 'import time;print(f" {(time.clock_gettime(time.CLOCK_MONOTONIC))}")'
The above shows the time in seconds with a decimal.
python3 -c 'import time;second=int(time.clock_gettime(time.CLOCK_MONOTONIC));print(f" {second} seconds")'
The above shows the time in seconds with no decimal.
python3 -c 'import time;second=int(time.clock_gettime(time.CLOCK_MONOTONIC));minute=second//60;print(f" {minute} minutes")'
The above shows the time in minutes.
python3 -c 'import time;s=int(time.clock_gettime(time.CLOCK_MONOTONIC));h=s//3600;m=(s-h*3600)//60;print(f"{h} hours {m} minutes")'
The above shows the time in hours and minutes.
python3 -c 'import time,datetime;print(datetime.timedelta(seconds=time.clock_gettime(time.CLOCK_MONOTONIC)))'
The above shows the time in hours, minutes, and seconds.
python3 -c 'import time,datetime;d=datetime.datetime(1,1,1)+datetime.timedelta(seconds=time.clock_gettime(time.CLOCK_MONOTONIC));print(f"{d.day-1} days, {d.hour} hours, {d.minute} minutes")'
The above shows the time in days, hours, and minutes.
I have used CLOCK_MONOTONIC
and Awk
to create a script that will calculate the total time if my Mint has been started more than once on the same date. The Awk
command can even be used to calculate the total time in one week/month/year.
Best Answer
Depending on your flavor of Unix, the
/proc
filesystem may have an uptime file somewhere with the information you want.And the output of the
uptime
command for the same time:So 5899847.37/86400 = 68.28527 --> 68 days, 6 hours, 51 minutes.