简体   繁体   中英

Tee command unix

How can I tee all these services into a log file:

stop service 1
stop service 2
stop service 3

I want all these services logs go to a file

should I use :

stop service 1 | tee log1.log
stop service 2 | tee log1.log
stop service 3 | tee log1.log

Or:

stop service 1 
stop service 2 
stop service 3 | tee log1.log

I haven't found a question like this Thank you!

tee accepts the -a flag to append to a file. So you should be able to redirect the output from all three commands to a single file by executing:

stop service 1 | tee log1.log
stop service 2 | tee -a log1.log
stop service 3 | tee -a log1.log

(The first command will truncate any existing file)

Another way not using tee is to simply use a bash command group with a single redirection of stdout to a log file:

{ stop service 1; stop service 2; stop service 3 } > log1.log

Use the >> operator to append to the log file, or use the &>> operator to append both stdout and stderr . It's also possible to use a bash subshell instead:

( stop service 1; stop service 2; stop service 3 ) > log1.log
{
stop service 1
stop service 2
stop service 3
} 2>&1 | tee log1.log

This treats the output of all three commands with a single redirection operation. It also includes the errors in the log file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM