16

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

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

0

4 Answers 4

32

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
1
  • 4
    Might be worth noting that this is bash built-in and GNU sleep behavior. POSIXly, sleep is only required to understand integers as seconds Aug 13, 2018 at 1:46
17

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.

10

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'
}
2
  • My remaining time is not working. andy@7_~$ remaining_sleep_time 7321 remaining_sleep_time: command not found
    – fixit7
    Aug 12, 2018 at 5:39
  • 2
    @fixit7 remaining_sleep_time needs to be copied into your ~/.bashrc file as per the link. You could also put the code into a bash script file. Aug 12, 2018 at 5:41
6

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

sleep 450

1
  • 4
    As noted previously , sleep indeed always understands seconds, but in case of GNU sleep and Bash shell ( not sure about ksh and zsh ) - those also allow letter arguments as noted in other answers. Your answer could be improved with using shell arithmetic expansion, so something like sleep $((60*7)) could be used for minutes Aug 14, 2018 at 18:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .