LinuxCommandLibrary

logsave

Record terminal output to a file

TLDR

Execute command with specified argument(s) and save its output to log file

$ logsave [path/to/logfile] [command]
copy

Take input from stdin and save it in a log file
$ logsave [logfile] -
copy

Append the output to a log file, instead of replacing its current contents
$ logsave -a [logfile] [command]
copy

Show verbose output
$ logsave -v [logfile] [command]
copy

SYNOPSIS

logsave [options] logfile command [arguments...]

PARAMETERS

-s, --size-limit size
    Limits the size of the log file. If the log file reaches or exceeds the specified size (in bytes), it is truncated to zero length before new output is written. This prevents log files from growing indefinitely.

-a, --append
    Appends output to the logfile if it already exists. Without this option, logsave will truncate the logfile (or create a new one) each time it is run.

-t, --time-format format
    Specifies the timestamp format using strftime(3) syntax. This allows for flexible control over how the timestamps appear in the log file. For example, %F %T for YYYY-MM-DD HH:MM:SS.

-v, --verbose
    Enables verbose mode, causing logsave to print additional diagnostic or informational messages to standard error.

-h, --help
    Displays a help message with usage information and exits.

-V, --version
    Displays version information for logsave and exits.

DESCRIPTION

The logsave command is a utility designed to execute a specified command and capture its standard output (stdout) and standard error (stderr) into a designated log file. Its primary feature is the automatic insertion of timestamps at the beginning of each line of output, making it exceptionally useful for monitoring long-running processes, cron jobs, or scripts where a chronological record of events is crucial.

Unlike simple redirection (e.g., command > logfile), logsave ensures that every line is prefaced with the exact time it was output by the wrapped command, providing valuable context for debugging, auditing, or performance analysis. It can either create a new log file or append to an existing one, and offers options for size limits and custom time formats. It is part of the util-linux project, a collection of essential Linux system utilities.

CAVEATS

logsave does not handle log rotation on its own; for advanced log management (like keeping multiple rotated logs), external tools such as logrotate(8) should be used in conjunction with logsave.

The accuracy of timestamps can be affected if the wrapped command buffers its output extensively; logsave adds the timestamp when a line is actually received, not necessarily when it was generated by the command.

Standard input to the wrapped command is passed through normally, but standard output and standard error are redirected to the log file.

EXIT STATUS

logsave returns the exit status of the command it executes. If the command cannot be executed (e.g., not found), logsave returns a non-zero exit status (typically 127 for command not found, or 126 for command not executable).

EXAMPLES

1. Basic usage to log a script's output:
logsave /var/log/myscript.log /usr/local/bin/my_daily_script.sh

2. Appending to an existing log file with a custom time format:
logsave -a -t "%Y-%m-%d %H:%M:%S" /var/log/apt_updates.log apt update && apt upgrade -y

3. Logging a command that runs indefinitely, with a size limit:
logsave -s 10M /var/log/app_monitor.log /usr/bin/some_monitoring_app --loop

HISTORY

logsave is a long-standing utility that forms part of the util-linux project, a collection of essential user-space utilities for Linux systems. Its purpose has remained consistent over time: to provide a robust and easy-to-use method for capturing time-stamped output of commands, particularly beneficial in unattended or background environments. Its inclusion in util-linux underscores its fundamental role in system administration and scripting, ensuring its widespread availability across Linux distributions.

SEE ALSO

logger(1), tail(1), cron(8), script(1), strftime(3), logrotate(8)

Copied to clipboard