3

Is there a way to check how long a running "sleep" process has left to go before it ends within the terminal?

4

The sleep command in itself offers no service that allows the monitoring of the remaining sleep time. It is also not displayed anywhere.

As the sleep command operates without issuing a series of system calls for the kernel, you won't be able to use a typical debugging tool such as dtruss either. If you try to use the lldb debugger to attach to the process, you'll find that the sleep command is also interrupted and cannot be continued.

So it is not as such possible to very precisely determine the amount of time left. If you can make do with an imprecise result, do the following:

First run this command:

ps xa | grep sleep

This should show you the sleep command with the number of seconds that it is supposed to sleep in total. For example:

1234 s0123 S+    0:00.01 sleep 789

Here 789 is the number of seconds that the command is supposed to sleep in total.

Then afterwards run this command:

ps xa -o etime,command -c | grep sleep

It will show you the total time the process has been running:

  03:15 sleep

This means that the process has sleep for 3 minutes and 15 seconds = 195 seconds. The remaining sleep time is 789 - 195 = 594 seconds.

| improve this answer | |
2

If your version of ps supports etimes (etime in seconds) (unfortunately not supported on MacOS), you can use this to calculate the remaining time:

ps xa -o etimes,command | awk '/[s]leep/ {print "time remaining for", $2, "is", $3 - $1, "seconds"}'

Example output:

time remaining for sleep is 94 seconds

Or you can simplify that to only show the number:

ps xa -o etimes,command | awk '/[s]leep/ {print $3 - $1}'

These commands will output more than one line if more than one sleep process is running.

Note: Neither etime nor etimes outputs fractional seconds, but sleep can accept them as an argument. This will likely affect the accuracy of your calculation.

For MacOS and others without etimes, you can convert etime (ddd-hh:mm:ss) to seconds reversing the calculations done in the source of ps (in print.c):

ps xa -o etimes,command | awk '
    BEGIN {
        f = "1 60 3600 86400";
        split(f, factors)
    }
    /[s]leep/ {
        split($1, parts, "[-:]");
        count = length(parts);
        for (i = count; i > 0; i--) {
            e += parts[i] * factors[count + 1 - i]
        };
        print e;
        e = 0
     }'
| improve this answer | |

You must log in to answer this question.

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