I'd like to use the time command in a bash script to calculate the elapsed time of the script and write that to a log file. I only need the real time, not the user and sys. Also need it in a decent format. e.g 00:00:00:00 (not like the standard output). I appreciate any advice.
Not quite sure what you are asking, have you tried:
time yourscript | tail -n1 >log
Edit: ok, so you know how to get the times out and you just want to change the format. It would help if you described what format you want, but here are some things to try:
time -p script
This changes the output to one time per line in seconds with decimals. You only want the real time, not the other two so to get the number of seconds use:
time -p script | tail -n 3 | head -n 1
From the man page for time: 1)There may be a shell built-in called time, avoid this by specifying /usr/bin/time 2)You can provide a format string and one of the format options is elapsed time - %E
/usr/bin/time -f'%E' $CMD
Example:
/usr/bin/time -f'%E' ls /tmp/mako/
res.py res.pyc
0:00.01
To use the Bash builtin time
rather than /bin/time
you can set this variable:
TIMEFORMAT='%3R'
which will output the real time that looks like this:
5.009
or
65.233
The number specifies the precision and can range from 0 to 3 (the default).
You can use:
TIMEFORMAT='%3lR'
to get output that looks like:
3m10.022s
The l
(ell) gives a long format.