Wednesday, February 18, 2015

Redirecting linux / unix time command output to a file

There may be requirement when you need to redirect unix / linux time command output to a file.

for me, -o option of time command did not work. Hence I used this - run command in a sub-shell and then redirect stdout and stderr into a file


>> All below did not work.

$ time -o /tmp/x date
bash: -o: command not found

real    0m0.001s
user    0m0.001s
sys     0m0.000s

$ time date
Wed Feb 18 08:18:41 UTC 2015

real    0m0.002s
user    0m0.000s
sys     0m0.001s
$ time date |grep real

real    0m0.002s
user    0m0.000s
sys     0m0.001s
$ time date 2>&1 |grep real

real    0m0.003s
user    0m0.000s
sys     0m0.002s
$ (time date )2>&1 |grep real
real    0m0.001s



>> Simple script.

$ cat /var/tmp/dns-check-script.sh
(
echo '----------------------------------------------------'
echo $((date;time -p host mysystem.com 10.10.10.11) &>/dev/stdout )
) >>/var/tmp/dns-check-script.log 2>&1



>> Run script via cron

$ cat /etc/cron.d/dns-check-script.cron
# Run script in every 2 minutes
*/2 * * * * root /var/tmp/dns-check-script.sh



>> This is how log will look like

#  tailf /var/tmp/dns-check-script.log
----------------------------------------------------
Tue Feb 17 12:28:01 GMT 2015 Using domain server: Name: 10.10.10.11 Address: 10.10.10.11#53 Aliases: mysystem.com has address 10.254.10.60 real 0.02 user 0.00 sys 0.00
Tue Feb 17 12:30:01 GMT 2015 Using domain server: Name: 10.10.10.11 Address: 10.10.10.11#53 Aliases: mysystem.com has address 10.254.10.60 real 0.02 user 0.00 sys 0.00



No comments:

Post a Comment