Recording and replaying terminal sessions in Linux can be done in a variety of ways, the following tools and methods are commonly used.
The Script command is a simple terminal session logger that can record terminal sessions and save them in a file to start recording sessions:
script -a mysession.txt
The -a option appends output to a file instead of overwriting it.
You can press Ctrl-D to end the recording session. To view session records, run the cat or less command:
cat mysession.txt
tmux and screen are two powerful terminal multiplexers that allow users to access the same session from multiple terminals and record the session. tmux logs a session to start a new tmux session:
Tmux
To start recording a session:
tmux start-server
tmux new -s mysession
tmux pipe-pane 'cat >> mysession.txt'
To stop recording a session:
tmux pipe-pane
To open a new screen session, record the session with screen:
screen -S mysession
Start recording:
:logfile mysession.txt
:log on
Stop recording:
:log off
asciinema can be used as an alternative to the script command to record terminal sessions, asciinema can record sessions and generate a shareable video of terminal sessions. Installation:
sudo apt-get install asciinema # Debian/Ubuntu
sudo yum install asciinema # CentOS/RHEL
Records:
asciinema rec mysession.cast
Play:
asciinema play mysession.cast
View and share sessions:
Upload the.cast file to asciinema.org, generating a web link that anyone can use to view your session online.
Make sure you don't disclose sensitive information such as passwords, API keys, etc., when logging a session.
The recorded session files can be large, especially long sessions, so clean up old session records regularly.
When using tmux and screen, make sure you are familiar with their commands and shortcuts to manage your session more effectively.
With these tools and methods, you can easily record and replay Linux terminal sessions. To automatically save terminal session records, run the following command:
script -a session.log
This command saves all output from the current session to the session.log file. To end the session log, press Ctrl-D.
You can also create a script to automatically log sessions, and create a script save_session.sh to start a new script session and save it to a time-stamped file.
#! /bin/bash
# Get the current date and time
timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
# Start a new script session and save it to a time-stamped file
script -a session_${timestamp}.log
Give script execution permission:
chmod +x save_session.sh
You can also periodically execute the save_session.sh script by setting the cron scheduled task. Open the current user cron table:
crontab -e
Add a cron task, such as saving session logs every half hour:
*/30 * * * * /path/to/save_session.sh
*/30 * * * * is executed every 30 minutes, and /path/to/save_session.sh is the script path.
If you need more sophisticated session management, such as keeping records periodically in an ongoing session, tmux or screen may be more suitable. You can set up a tmux session and use tmux's clock-mode function to periodically save logs.
Note that you should make sure that script commands are available on your system. If not, you may need to install the bsdmainutils package. When using cron, make sure that your script path is correct and that the script has execution permissions. Check log files regularly to make sure they are created and saved correctly. With disk space in mind, clean or archive old log files regularly.
In this way, you can set up an automated system to periodically save your terminal session records.