My aim is to log all output from a script into a directory that the script is going to create.
For example, I have:
~/.abc.sh:
#! /bin/bash
rails new myappWhen I run...
cd ~/code
. ~/.abc.sh...that will create a new Rails app in directory ~/code/myapp.
When Rails is creating an app, it outputs a whole lot of text that I want to capture and store in a log file in the same directory the rails command newly created. I also want to display that text in the terminal as well.
How do I go about doing this?
2 Answers
You can use the tee command for that:
command | tee /path/to/logfileThe equivelent without writing to the shell would be:
command > /path/to/logfileIf you want to append (>>) and show the output in the shell, use the -a option:
command | tee -a /path/to/logfilePlease note that the pipe will catch stdout only, errors to stderr are not processed by the pipe with tee. If you want to log errors (from stderr), use:
command 2>&1 | tee /path/to/logfileThis means: run command and redirect the stderr stream (2) to stdout (1). That will be passed to the pipe with the tee application.
script will start an interactive session and log all the output (stdout/stderr etc) to a file, or (with the -c parameter) will run a command and log the output of that.
script -c ~/.abc.sh -f abc.logNote: in an interactive session, you can stop recording just by exiting the session as you normally would (e.g. exit or Ctrl-D).
For session recording with video playback, you can also try asciinema.