Send Output to Two Destinations with tee

Introduction

The tee command is an essential Unix utility that reads from standard input and writes simultaneously to standard output and one or more files. In complex shell workflows, tee allows you to split data streams on the fly—enabling logging, debugging, auditing, and real‐time monitoring.

Basic Usage

Syntax

Component Description
tee [options] ltfile1gt [ltfile2gt …] Read from STDIN write to STDOUT and listed files.

Common Options

  • -a: Append to the given files rather than overwrite.
  • -i: Ignore interrupts.
  • –help: Show usage information.

Practical Examples

1. Capturing Build Logs

make 2>1  tee build.log

This records both stdout and stderr into build.log while still displaying progress on-screen.

2. Appending Data

echo New entry  tee -a logfile.txt

3. Duplicating to Multiple Files

ps aux  tee processes.txt backup/processes.txt

Advanced Techniques

Process Substitution

Leverage Bash’s process substitution to feed outputs into multiple processors without intermediate files:


diff -u 

Named Pipes (FIFOs)

Create a named pipe and tee into it for inter-process communication:


mkfifo mypipe
generate-data > mypipe 
tee logfile.txt 
consumer-process 

Using tee with sudo

Standard redirection under sudo fails because redirection is handled by the shell. Use tee to work around:


echo configuration  sudo tee /etc/myapp.conf > /dev/null
  

Real-World Scenarios

1. Deployment Logging

In CI/CD pipelines, capture output from build stages:


./deploy.sh 2>1  tee /var/log/deploy-(date  %F).log
  

2. Monitoring VPN Connection Logs

When debugging VPN tunnels, tee can split live logs to screen and file. For example, to watch an OpenVPN session:


sudo openvpn --config client.ovpn 2>1  tee vpn-openvpn.log
  

Similarly, capture WireGuard activity:


sudo wg-quick up wg0 2>1  tee vpn-wireguard.log
  

For commercial clients like NordVPN or ExpressVPN, integrate their CLI commands into tee pipelines to maintain historic logs.

Security Considerations

  • Sensitive Data: Avoid tee’ing credentials or private keys to world-readable files.
  • File Permissions: Use umask or chmod after tee to lock down logs.
  • Disk Usage: Monitor log file growth implement log rotation (e.g., via logrotate).

Performance Impact

While tee adds minimal overhead, writing to slow storage (network mounts, HDD) can bottleneck pipelines. Benchmarks show sub-millisecond delays per write on SSDs, but consider asynchronous strategies (e.g., piping into xargs -P or backgrounded tee processes).

Tool Comparison

Utility Primary Function Pros Cons
tee Duplicate streams Simple, built-in, versatile Limited filtering
script Record entire terminal session Captures interactive input Large output, less selective
multilog Supervise log daemons Automated rotation Requires daemontools

Tips Best Practices

  • Group pipelines: (cmd1 cmd2) tee combined.log
  • Use time-stamped logs: tee (date %Y%m%d_%H%M%S).log
  • Rotate and compress logs via cron or logrotate.
  • For high-throughput, consider pv in the pipeline to monitor rate.

Conclusion

The tee command may seem trivial at first glance, but its power in splitting data streams becomes invaluable in scripting, system administration, and real-time monitoring. Whether capturing build logs, debugging VPN tunnels with OpenVPN or WireGuard, or simply appending entries to multiple files, tee streamlines workflows and enhances visibility into your processes.

Download TXT



Leave a Reply

Your email address will not be published. Required fields are marked *