1

I have a tool that outputs to standard output every so often, without loss of generality, let's say that the sequence goes:

mysystem$ ./runme
Starting
Done
mysystem$

Now 'runme' takes a while to complete so I'd really like to get the shell to give me

mysystem$ somecommand ./runme
0952 Starting
1134 Done
mysystem$

Or some approximation of this... any ideas? Would be happy with things like writing to file and so on...

EDIT love the quick response so far but I should have been clearer...

There may well be intermediate statements made by the program so I'd really like to get the shell to give me

mysystem$ somecommand ./runme
0952 Starting
0959 Somewaythough
1011 Mostly done
1134 Done
mysystem$

But this may well be impossible...

1
  • 1
    You're asking two completely separate questions. Your edited post requires that you actually have output, while the first one could do without. For example, sleep 5 cannot be timed the way you want it to. Please clarify which it is.
    – Daniel Beck
    Nov 10, 2011 at 17:18

5 Answers 5

2

Current

$ ./slow
Starting ...
Nearly finished ...
All done.

Desired

$./slow | ./stamp
15:31:19 Starting ...
15:31:29 Nearly finished ...
15:31:31 All done.

Slow prog

$ cat slow
#!/usr/bin/ksh
echo Starting ...
sleep 10
echo Nearly finished ...
sleep 2
echo All done.

Timestamper

$ cat stamp
#!/usr/bin/perl
use strict;
use warnings;
while(<>) {
  my($sec, $min, $hour) = (localtime)[0..2];
  print "$hour:$min:$sec $_";
}

Actually, I'm surprised this works, I thought I'd have to do something about buffering. YMMV

Using sprintf to format the time is left as an exercise for the reader.


For golfers

$ ./slow | perl -n -e 'printf "%02d:%02d:%02d %s", (localtime)[2,1,0],$_'
15:42:17 Starting ...
15:42:27 Nearly finished ...
15:42:29 All done.
2

You can put time in front of the command to execute and it will time the execution.

user@hostname/pwd$ time ls
file1
file2
file3

real      0m0.006s
user      0m0.001s
sys       0m0.003s

Longer commands make for more interesting timings.

2
  • Make this an answer @slhck - this fits best with the OP
    – Paul
    Nov 10, 2011 at 10:48
  • Didn't the Q ask for "Timestamps" with the hours and minute at which an event occurred? Nov 10, 2011 at 15:23
0

What about:

ls -la 
total 28
drwxr-xr-x   2 glens glens    27 2011-06-16 09:27 ./
drwxr-xr-x 197 glens glens 69632 2011-11-10 11:17 ../
-rwxrwxrwx   1 glens glens   406 2011-06-16 09:27 SERVER_LOG.txt*

and with sed

ls -la  | sed -e "s/\(.*\)/$(date +%H%M) \1/"
1154 total 28
1154 drwxr-xr-x   2 glens glens    27 2011-06-16 09:27 ./
1154 drwxr-xr-x 197 glens glens 69632 2011-11-10 11:17 ../
1154 -rwxrwxrwx   1 glens glens   406 2011-06-16 09:27 SERVER_LOG.txt*
0

In bash, you can combine a DEBUG trap and PROMPT_COMMAND to print the time directly before and after executing a command.

trap 'echo $( date )' DEBUG
PROMPT_COMMAND='echo $( date )'

This is how it looks like (second output repeated, as PROMPT_COMMAND is a trappable command:

$ sleep 5
Do 10 Nov 2011 18:10:36 CET
Do 10 Nov 2011 18:10:41 CET
Do 10 Nov 2011 18:10:41 CET
1
  • I saw that the user also wants times of intermediate statements, but this might be useful for somebody anyway.
    – Daniel Beck
    Nov 10, 2011 at 17:13
0

To show one nice timestamp before the command and another after it:

### show current time for each command
## after the command:
PS1='[\t]'"$PS1"
## before the command:
trap 'echo [$( date +%H:%M:%S)]' DEBUG

Output example:

me@mycomp MINGW64 ~/Projects/myproject (master)
$ php -v
[16:03:41]
PHP 8.0.14 (cli) (built: Dec 16 2021 10:25:26) ( ZTS Visual C++ 2019 x64 )
Copyright (c) The PHP Group
[16:03:41]

You must log in to answer this question.

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