Home > File & Directory Management > tee

tee -a -i: Append to file and ignore interrupts

The `tee` command is a utility that reads from standard input and writes to standard output and one or more files simultaneously. This specific combination (`-a -i`) appends content to files instead of overwriting them and ignores interrupt signals (SIGINT, typically Ctrl+C), preventing the `tee` process from being terminated. This functionality is highly useful for continuously logging output from long-running scripts or when preserving critical output is necessary.

Overview

The `tee` command acts as an intermediary in a pipeline, intercepting data to save it to a file while simultaneously passing it to the next command. The `-a` option preserves existing file content and appends new content to the end of the file, while the `-i` option prevents the `tee` process from being forcibly terminated by user interrupts, enabling stable logging.

Key Features

The key features of this combination are:

  • Simultaneously writes standard input to standard output and specified files.
  • Appends to file content instead of overwriting (-a option).
  • Ignores interrupt signals (SIGINT) (-i option).
  • Suitable for stable logging of long-running processes.

Key Options

The `tee` command offers various options; here, we explain the core options used in the `tee -a -i` combination.

File Output and Behavior Control

Generated command:

Try combining the commands.

Description:

`tee` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Practical usage examples of the `tee -a -i` command.

Appending Command Output to a Log File

ls -l | tee -a -i output.log

Appends the output of the `ls -l` command to `output.log` while simultaneously displaying it on the terminal. Pressing Ctrl+C will not terminate `tee`.

Continuously Logging Script Execution Results

while true; do echo "$(date): Running..."; sleep 1; done | tee -a -i script_log.txt

Continuously appends the output of an infinite loop script to `script_log.txt`. Thanks to the `-i` option, `tee` is unaffected by interrupt signals.

Logging Output of sudo Commands

sudo sh -c 'apt update && apt upgrade -y | tee -a -i /var/log/apt_updates.log'

When logging the output of commands executed with `sudo` to a file, it's safer to use `sh -c` to capture the actual command's output rather than `sudo`'s own output with `tee`.

Tips & Precautions

Useful tips and points to consider when using the `tee -a -i` combination.

Usage Tips

  • **Logging Background Tasks**: For logging the output of long-running background processes to a file, use `nohup command | tee -a -i logfile.log &` to establish a stable logging environment.
  • **Resolving Permission Issues**: When using `sudo command | tee file`, `tee` might run with regular user privileges, leading to file write permission errors. In such cases, you should execute `tee` itself with `sudo`, for example: `sudo sh -c 'command | tee -a -i file'` or `command | sudo tee -a -i file`.
  • **Debugging**: In complex pipelines, to inspect the output of intermediate stages, you can use `command1 | tee -a -i debug.log | command2` for debugging purposes.

Precautions

  • **File Size Management**: Continuously appending to a file with the `-a` option can lead to an ever-increasing file size. Consider using log rotation tools (e.g., `logrotate`) or periodically backing up log files.
  • **Meaning of Ignoring Interrupts**: The `-i` option prevents the `tee` process itself from terminating, but it does not prevent preceding commands piped to `tee` from terminating. If a preceding command terminates in response to an interrupt, `tee` will no longer receive input.

Same category commands