Thursday, August 22, 2013

redirect stdout and stderr to a file

program [arguments...] 2>&1 | tee -a outfile &

2>&1: directs stderr to stdout

tee receives the content from stdout and write them to a file. -a means append
see man tee for more info.

E.g.,

I want to append the openvz migration log message to a file. This shell script migrates 100 openvz containers in parallel, and wait all of them to finish. It outputs the time at the end.

#!/bin/sh

echo "*********************************************************" | tee -a migrationResult.txt


date | tee -a migrationResult.txt


for i in {1..100}

do
        vzmigrate --live -t  sr1s1 $i 2>&1 | tee -a migrationResult.txt &
done
wait // wait all of the processes to finish
date | tee -a migrationResult.txt

No comments: