0

I have a process that I want to log how long it took. I currently view the time output which looks like:

$ time ls

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

I want to capture the elapsed time (the "real" entry) and record it in a database (or text file, or whatever), but I don't know how to do that without also redirecting the standard error output of the command being timed, which I want to avoid.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
user9170
  • 950
  • 9
  • 18
  • probably relevant: http://stackoverflow.com/questions/2342826/how-to-pipe-stderr-and-not-stdout – Marc B Sep 30 '15 at 17:04
  • 1
    There are duplicates for this. It is surprisingly hard. IIRC, `(time ls) 2>time.log` will put the output of `time` into `time.log`. – Jonathan Leffler Sep 30 '15 at 17:05
  • also: http://stackoverflow.com/questions/26784870/parsing-the-output-of-bashs-time-builtin (especially my answer that will show you how to format the output of `time`). – gniourf_gniourf Sep 30 '15 at 17:05
  • See also [Bash script — writing executing time in a file](http://stackoverflow.com/questions/13176611/bash-script-write-executing-time-in-a-file/). It's an earlier duplicate; I don't know that it's the earliest. – Jonathan Leffler Sep 30 '15 at 17:08
  • @JonathanLeffler Indeed it's an earlier duplicate... (I only remembered the question I answered and wasn't really aware of the older one) – gniourf_gniourf Sep 30 '15 at 17:11
  • 1
    @gniourf_gniourf: you were just the FGITW; the one I suggest has an answer from me, which is why _I_ remember it. The main thing is the OP has (multiple) answers and people don't spend lots of time repeating what's already available information on SO. – Jonathan Leffler Sep 30 '15 at 17:13
  • @JonathanLeffler as an apology, please accept my upvote on your answer `:)`. – gniourf_gniourf Sep 30 '15 at 17:14
  • Unfortunately, I need the file descriptors unmodified, so those solutions won't work. The command after time ssh's in to a server, and the 2>&1 messes up the process. the file descript redirection sends everything, not just time's output. – user9170 Sep 30 '15 at 18:14
  • I might just have to get a before and after time and pipe it to calc. – user9170 Sep 30 '15 at 18:24
  • `X=`date +%s`; ssh remotehost ; Y=`date +%s`; echo "$Y-$X" | calc -p > out.calc ` works – user9170 Sep 30 '15 at 18:37
  • The way your question is asked is answered exactly in the question given as a link, as my answer there also shows how to preserve stdout and stderr of the command. If you're having a specific problem with `ssh`, then please ask another question, mentioning this one (or the duplicate) and explain why it doesn't solve your problem. – gniourf_gniourf Sep 30 '15 at 19:40
  • gniourf_gniourf , I tried your command, it did not work for me. – user9170 Oct 06 '15 at 15:55

0 Answers0