This article has participated in the call for good writing activities, click to view: back end, big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!

A lifelong learner, practitioner and sharer committed to the path of technology, a busy and occasionally lazy original blogger, an occasionally boring and occasionally humorous teenager.

Welcome to dig friends wechat search “Jie Ge’s IT journey” attention!

The original link: SAO operation | how to grace under the Linux terminal video?

preface

I believe that when you search for the history command in the initial Linux operating system, you will immediately think of the history command: it will display the command that has been operated before, but it cannot display the output process and result of the command.

But sometimes we wonder what’s going on on this server? What is the process of implementation? What is the output? The history command no longer meets our needs, so what do we do?

It’s time to teach you a lesson. That’s the magic command script and scriptreplay

Record — Script

Script command: you can record the session on the Linux terminal.

Function: All operations, executed commands, and output results on the terminal can be recorded locally.

Script and Scriptreplay are installed on Linux distributions by default, and in this article script can be executed directly on the operating system, while Scriptreplay requires an additional installation.

Start the video

If you run the following command, any operation performed on the terminal will be recorded.

# script -t 2>test.timefile -a test.txt
Script started, file is test.txt
Copy the code

Check whether the following two files exist in the current directory

# ll-rw-r --r--. 1 root root 1025 6月 4 07:32 test.timefile -- rw-r--r--. 1 root root 5772 6月 4 07:32 test.txtCopy the code

Timefile and test. TXT file names can be customized.

  • test.timefile: mainly used to record sequence files, which record the specific execution time of each command;
  • test.txt: Stores command output files that have been executed.

Common Parameter Options

  • -t: Used to specify the time of output recording.
  • -a: Used to output the recorded file and add new content to the existing content;
  • -c: shell for direct command execution, not interactive;
  • -f: Allows you to view the contents of log files while outputting log files.
  • -q: used to run script commands in silent mode;
  • -V: Used to display the version and exit;
  • More related detailed parameter options can be executedman scriptCommand to view;

You can run the following command in silent mode without script and exit for startup and exit.

# script -q -t 2>test.timefile -a test.txt
Copy the code

To exit the recording, run the exit command or press Ctrl+D.

# exit Exit Script done on Saturday, June 04, 2016 07:32:42 SECCopy the code

Record a script execution case in a directory

Start by writing a simple script file named xxx.sh and granting the related permissions.

# vi helloworld.sh #! /bin/bash echo hello world! echo welcome to beijing! echo Let's go to tian 'anmen square!Copy the code

Using the option of -c, you can run the command directly instead of the interactive shell to view the time when the script is executed.

# script -qa "file.out" -c "/root/helloworld.sh" hello world! welcome to beijing! Lets go to tian anmen square! [root@localhost ~]# cat file. Out Script started on 2016年06月04日 sat 13:21:45 秒 Hello world! welcome to beijing! Lets go to tian anmen square!Copy the code

Execute related command operation and record demonstration

Ii. Playback — Scriptreplay

Scriptreplay command: can play back the recorded results in Linux terminal.

Common Parameter Options

  • -t: a file that contains a time sequence;
  • -d: used to accelerate the playback speed multiple (can be decimal: slow down); Some versions may not have this parameter;
  • -f: refreshes the cache immediately after each operation. If this option is not set, files will not be written in real time.
  • -s: used to contain the output of the script terminal;
  • -V: Used to display the version and exit;

Install scriptreplay

Util – Linux – ng – 2.17.2. Tar. Gz package download path: mirrors.edge.kernel.org/pub/linux/u…

# tar ZXVF util-linux-ng-2.17.2.tar.gz # CD util-linux-ng-2.17.2 #./configure && make # cp misc-utils/scriptreplay /usr/bin/cp: Override "/usr/bin/scriptreplay"? y # cd .. # scriptreplay test.timefile test.txtCopy the code

Run the scriptreplay test.timefile test. TXT command to play back the previous operations.

Synchronization using

Use the -f parameter option. Some versions do not have this parameter. You can also run the tail -f command to perform operations.

As shown in the figure below, two Xshell terminals need to be opened, connected to the same device, and related synchronous operation is performed. The left screen is: Operation terminal, the right screen is: demonstration terminal,

Run the script -f jacktian command on the left terminal

On the right screen, run tail -f jacktian

Then, you only need to execute the relevant commands in the left screen terminal, and the right screen will display the relevant output results of the left screen, etc.

Self-start upon startup

In addition to the above, you can set script commands to be automatically executed at login and added to the shell environment configuration file.

Create the directory /var/log/user_record recursively

# mkdir -p /var/log/user_record
Copy the code

Edit the /etc/profile file to add the following self-starting configuration items

# vi /etc/profile

script -t -f -q 2>/var/log/user_record/$USER-$UID-`date +%Y%m%d%H%M%S`.time -a /var/log/user_record/$USER-$UID-`date +%Y%m%d%H%M%S`.who
if [ "$SHLVL" = 1 ]; then
   exit
fi
Copy the code

conclusion

  • Record the command as a video file and save it to the local PC.
  • The command output file results can be stored and directly shared with friends.
  • You can view the detailed execution process and related errors by saving the output file results of executed commands.
  • When you encounter difficulties and need others to assist you in the operation, prevent the execution process from being too fast, start recording and watch the playback;
  • Start recording at any time to monitor the related operations done by others connected to the device;

Original is not easy, if you think this article is useful to you, please kindly like, comment or forward this article, because this will be my power to output more high-quality articles, thank you!

By the way, please give me some free attention! In case you get lost and don’t find me next time.

See you next time!