How to Record Your Work on the Command Line with the script(1) Command
Most Unix-like operating systems feature a script command. You can find its manual in script(1) (type man 1 script
to access it). script
records a transcript (“typescript,” not to be confused with the language TypeScript) of your current session in the command line.
The script command can be used as a way to log what you are doing in a shell session. It’s often used during troubleshooting, documentation, PCI compliance audits, security/remediation work, penetration tests, and other situations where it’s useful to record a play-by-play log of what you’re doing on the machine.
Practical Demonstration
How to use it
All you have to do is to run it:
$ script
Script started, output file is typescript
This is especially useful if you want to have a journal for yourself or as a way to document what you did for others.
If you don’t like the name typescript or want to save your output file to a specific path, you can do so by supplying a path as the argument. For example:
$ script ~/what_you_did_last_summer.log
This will record the input you type into your shell and the output you receive, and write both into the what_you_did_last_summer.log in your home directory.
The command comes with a number of flags. Here are a few useful ones:
- -a to append the output, rather then replacing the output file
- -t to add timestamps to the log, which allows it to be played in real time
The flags which you have available will depend on the operating system you’re using(specifically, the implementation of the command used in your operating system), so it’s useful to run man 1 script
to familiarize yourself.
Stopping the recording
To stop recording the script all you have to do is what you would normally do to get out of a shell session. That means using the commands exit, logout or the CTRL-d keyboard shortcut. All of these end the recording, but won’t close your actual session as they normally would.
Conclusion
The script command can be your to-go tool for documenting your work and showing others what you did in a session. Have a complex process at your work place? Want to give an example on how to do stuff to your current or future coworkers? Just make a recording and share the information using script
. That way your team can profit from it, even while you’re on vacation (or just busy trying to find a root cause for the latest server outage).
script
also provides a simple way to get information for debugging or troubleshooting. There are plenty of use cases! You can:
- Pass information to a remote coworker.
- Prove that a specific version of a library is installed on all your servers for a PCI Audit.
- Record everything you see while logging into a server that’s been compromised by a ‘hacker’.
- Ask a person that you’re helping over the Internet to make a recording via
script.
This is especially useful when a problem that can’t easily be tracked down by copy pasting a single line, or because it involves several commands and/or a lot of output. - Record a way to replicate a hard-to-debug issue where timing is relevant.
This simple-to-use command is a great way to share what’s happening in your terminal.
Alternative: asciinema
script(1) has the benefit of existing on many operating systems, and likely already exists on your server. If it’s not there already, you can easily download a prebuilt binary from a repository that’s considered secure. However, there’s an alternative that can be useful to know about: asciinema provides a way to create something closer to a YouTube video or animated GIF than a simple textfile. This is much like making a script
recording with timestamps and playing it back later, and makes it easier to embed in HTML documents.
That’s it — have a great time recording your sessions!