The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 6,067β€…    Votes:  10β€…
Tags: command-line   coreutils  
Link: πŸ” See Original Answer on Ask Ubuntu ⧉ πŸ”—

URL: https://askubuntu.com/q/1064568
Title: Use sleep with minutes and seconds
ID: /2018/08/12/Use-sleep-with-minutes-and-seconds
Created: August 12, 2018    Edited:  August 12, 2018
Upload: April 8, 2024    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


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'
}
⇧ Laptop CPU+GPU overheating after update to 18.04 LTS Serious screen flickering when WiFi is on (Dell XPS 15)  β‡©