4

Say, if we run some long command, such as a functional test for a website, is there a command such as:

elapsed npm test

where npm test is a process or shell script possibly with several different commands that can take 45 minutes. It'd be nice if during the running, it can print out "22 minutes have elapsed" every minute or every 5 minutes.

The command time can show the total time it takes, but not the time elapsed along the way.

3
  • from another windows, get its pid, then use ps -p PID -o etime (I wrote as a comment as it is not a direct answer, I wouldn't expect a command to exists, yet ...)
    – Archemar
    Jun 30, 2017 at 13:11
  • Can't you use %E for time command ?
    – Thushi
    Jun 30, 2017 at 14:47
  • Do any of the existing answers solve your problem? If so, please indicate so with the checkmark, or else let us know what's missing. Thank you!
    – Jeff Schaller
    Jul 9, 2017 at 15:20

3 Answers 3

4

You could use pv -t:

npm test | pv -t

Or to avoid npm's output going through pv, use a fd that npm is not writing to:

{ npm test 3>&1 >&4 4>&- | pv -t; } 4>&1
3

Start the command in the background and poll its elapsed CPU time every minute:

npm test &
while sleep 1m
do
    ps -o etime $!
done
1

Along the lines of l0b0's answer: put the job in the background, but then run a busy-loop around the question "is it still running?", emitting elapsed minutes along the way:

elapsed

#!/bin/sh
start=$(date +%s)
"$@" &
while ps -p $! > /dev/null
do
  sleep 1
  now=$(date +%s)
  if [ $(( (now - start) % 60)) -eq 0 ]
  then
    printf "%d minutes have elapsed\n" $(( (now - start) / 60))
  fi
done

This runs the risk of another process reusing the same PID as our background process, if our process ends and that other process begins while we're sleeping.

To report the elapsed minutes every 5 minutes instead of every minute, just change the first 60 to a 300.

You must log in to answer this question.

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