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.
if the user is allowed to use at
command, this is the perfect use for that:
$ at 08:00 022116
at> myscript.sh
at> <----------- ctrl-d here
job 9 at 2016-02-21 08:00
if you get a message like "user blah is not able to run at
", ask the syadmin to add this user to at.allow
file or remove from at.deny
file, depending on how it is used in your environment.
Unless of course you wish this days long sleep happen in the middle of your script. Even then you can cut up your script into two, writing variables you want to keep, to a file and when the time comes and second part of your script executes, it can read those variables from the stored file.
Best Answer
Supporting GNU or Solaris 11
sleep
arguments (one or more<double>[smhd]
durations, so would also work with traditional implementations that support only one decimal integer number (like on FreeBSD), but not with those accepting more complex arguments like ISO-8601 durations). Usingetime
instead ofetimes
as that's more portable (standard Unix).(the
s/\?//g
is to get rid of the?
characters thatprocps
'ps
uses as replacement for control characters. Without it, it would fail to parsesleep $'\r1d'
orsleep $'\t1d'
... Unfortunately, in some locales, including theC
locale, it uses.
instead of?
. Not much we can do in that case as there's no way to tell a\t5d
from a.5d
(half day)).Pass the pid as argument.
That also assumes the
argv[0]
passed tosleep
doesn't contain blanks and that the number of arguments is small enough that it's not truncated byps
.Examples:
For a
[[[ddd-]HH:]MM:]SS
output instead of just the number of seconds, replace theprint $t
with: