Use sleep with minutes and seconds

I know how to use sleep 5m for 5 minutes.

How would I use it for say 7 mins 30 seconds?


Solution 1:

From the manpage:

Given two or more arguments, pause for the amount of time specified by the sum of their values.

So:

sleep 7m 30s

Solution 2:

Another way to do this is:

sleep 7.5m

man sleep

Unlike most implementations that require NUMBER be an integer, here NUMBER may be an arbitrary floating point number.

Solution 3:

There are two excellent answers already but I'd like to expand a bit. Copy and paste command below into your terminal:

$ sleep 3d 5h 7m 30.05s &
[1] 7321

This will start a second process that will sleep for 3 days, 5 hours, 7 minutes and 30.05 seconds. The process ID (PID) is 7321 in my case.

To confirm the PID use

$ ps aux | grep sleep
rick      7321  0.0  0.0  14356   660 pts/2    S    22:40   0:00 sleep 3d 5h 7m 30.05s
root     12415  0.0  0.0  14356   700 ?        S    22:41   0:00 sleep 60
rick     12500  0.0  0.0  21292   968 pts/2    R+   22:41   0:00 grep --color=auto sleep

The first entry is the one we are interested in. The second entry is for a permanent program I have running in startup. The third entry is for the grep command itself.

Now to see how much time is remaining (in seconds) for the sleep command generated by PID 7321 we can use this: How to determine the amount of time left in a “sleep”? command:

$ remaining_sleep_time 7321
277304.05
$ remaining_sleep_time 7321
277296.05
$ remaining_sleep_time 7321
277262.05

The code for the command you can include in your ~/.bashrc file:

remaining_sleep_time() { # arg: pid
  ps -o etime= -o args= -p "$1" | perl -MPOSIX -lane '
    %map = qw(d 86400 h 3600 m 60 s 1);
    $F[0] =~ /(\d+-)?(\d+:)?(\d+):(\d+)/;
    $t = -($4+60*($3+60*($2+24*$1)));
    for (@F[2..$#F]) {
      s/\?//g;
      ($n, $p) = strtod($_);
      $n *= $map{substr($_, -$p)} if $p;
      $t += $n
    }
    print $t'
}

Solution 4:

Command sleep is always counting in seconds. You could use ((7x60)+30) seconds and then type simply:

sleep 450