I can’t believe I didn’t know about this command beforehand. When I first got into self-hosting and homelabbing, one of the app ideas I had that would help me with internal documentation is a terminal program that saves your input and output to a file for you to review later and extract the key commands you used. I also had the bonus idea that you could add comments as you were writing out commands.
Little did I know at the time that comments from the CLI were already possible! I’ve already begun using comments which has been helpful if I need to look back at my zsh_history
file. Here’s an example of a command I would use with my docker services.
docker logs {container_name} --since 5m ;: The container is failing 5 minutes after startup.
You can write comments in bash
and powershell
as well!
- Powershell:
rem
- ZSH:
;:
- Bash:
#
Turns out, I don’t need a fancy app to log the input/output of a terminal session! It exists in most unix based systems already and the command is simply this: script
. That’s it!
Unlike .zsh_history
or .bash_history
which only saves the commands you input into the terminal, script
will save both your commands and it’s output. Why didn’t I know about this before I started setting up my homelab?!
The next time I want to setup a new service or debug something on a server, I will now make sure I start my session with script {date}-{service_name}.txt
and then start writing out commands. As long as I can remember to write my inline comments during the session, looking back and trying to figure out what I was thinking at the time should be a breeze! I can dump these files into my internal wiki as placeholders. Then, ideally, I’ll remove the commands that lead me down a dead end, clean up and expand comments, and easily keep my wiki growing. Ideally being the key word here… I’ll take a dump of history files for the time being.
Either way, I will literally have the output of my brain when doing CLI work in a file. Incredible.
Here’s a quick overview about script, taken from man script
.
SCRIPT(1)
NAME
script – make typescript of terminal session
SYNOPSIS
script [-aeFkqr] [-t time] [file [command ...]]
script -p [-deq] [-T fmt] [file]
DESCRIPTION
The script utility makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be
printed out later with lpr(1).
If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript.
If the argument command is given, script will run the specified command with an optional argument vector instead of an interactive shell.