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?

4
  • 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
    Commented Mar 22, 2016 at 7: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
    Commented Mar 22, 2016 at 8:12
  • stackoverflow.com/questions/8903239/…
    – Osaid
    Commented Mar 22, 2016 at 8:21
  • close, but none of the suggested answers combine a readable form of the date with the elapsed time. Commented Mar 22, 2016 at 9:34

1 Answer 1

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

You must log in to answer this question.

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