1

I can save and view the cygwin console output (stdout and stderr) using the following command,

python command.py 2>&1 | tee -a outFile.txt// Note -a is for appending

since I have different commands and they take different time to execute. How can I save the start time like

09:00:00AM
file stream file stream file stream file stream 
file stream file stream file stream file stream 
.
.
.
file stream file stream file stream file stream 
09:07:20AM

Note the start and end time stamp. It would be awesome if I could log the diff time

Total execution time: 00:07:20

Yes I can get the time using date command in cygwin,

$ date -> Tue, Mar 22, 2016  12:00:47 AM

but do I have to parse this date to get the time in variable and then take diff to get time elapsed?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Osaid
  • 151
  • 7
  • Maybe there is another option. You could use the command `time` to print timing statistics, eg. `time python command.py 2>&1 | tee -a outFile.txt`. Use `man time` for more info. I'm not sure if this is available on `cygwin`. – mnille Mar 22 '16 at 07:39
  • Try to execute this `time ls`. What is the output? For me, it's something like this: `file1 file 2 file3` `real 0m0.003s` `user 0m0.003s` `sys 0m0.000s` – mnille Mar 22 '16 at 08:12
  • http://stackoverflow.com/questions/8903239/how-to-calculate-time-difference-in-bash-script – Osaid Mar 22 '16 at 08:21
  • close, but none of the suggested answers combine a readable form of the date with the elapsed time. – Thomas Dickey Mar 22 '16 at 09:34

1 Answers1

1

The following SEQUENCE of commands I have tried to achieve the start/end and elapsed time in the log file. There might be a better way of doing this.

date 2>&1 | tee -a outFile.txt && SECONDS=0 && command.py 2>&1 | tee -a outFile.txt && date 2>&1 | tee -a outFile.txt && duration=$SECONDS echo "Total Execution Time: $(($duration / 60)) m $(($duration % 60)) s" 2>&1 | tee -a outFile.txt
  1. Its just getting start date and appending to file,

    date 2>&1 | tee -a outFile.txt 
    
  2. then executing command and appending to file,

    && SECONDS=0 && command.py 2>&1 | tee -a outFile.txt
    
  3. then getting end date and appending to file.

    && date 2>&1 | tee -a outFile.txt
    
  4. and at last calculating elapsed time and appending to file

    && duration=$SECONDS echo "Total Execution Time: $(($duration / 60)) m $(($duration % 60)) s" 2>&1 | tee -a outFile.txt
    

The following is the output of the log file,

    Tue, Mar 22, 2016  4:01:18 PM

    file stream file stream file stream file stream 
    file stream file stream file stream file stream 
    .
    .
    .
    file stream file stream file stream file stream 
    Tue, Mar 22, 2016  4:01:23 PM
    Total Execution Time: 0 m 7 s
Osaid
  • 151
  • 7