When we debug the program, we have to grab some logs and analyze them. If you don’t have a lot of logs, it’s easy, just copy and paste. But what if you do some stress testing, generate a lot of logs, and have a small amount of memory (for example, on an embedded device)?
Of course, secureCRT and mobaXterm both have the ability to save logs locally and are easy to use. Some tools, such as putty, do not.
This is where the recorder script in the terminal comes in handy.
Usage scenarios
- Debugging log-generating applications that need to be saved locally for further analysis;
- Cooperate with colleagues to finish half of the work, can record the operation process, send to colleagues, colleagues can continue to work according to the record;
- Let a person assist you remotely, worry about the other side bad, also can leave a record at the same time, had better record his operation
How do I use script commands?
By default, you simply typescript, which automatically creates a typescript file in the current directory where everything you do on the terminal is recorded.
The record file is a text file that can be opened and viewed using any text tool.
To exit the record, press CTRL + D in the terminal or enter Exit. Before exiting script, you will notice that the record file size is 0 Kb, and when exiting, the file size will increase.
[alvin@VM_0_16_centos test]$ script Script started, file is typescript [alvin@VM_0_16_centos test]$ echo hello hello [alvin@VM_0_16_centos test]$ ls test1.py test2 test2.cpp test2.py test3 test3.c test.py typescript WeixinBot wxpy wxRobot [alvin@VM_0_16_centos test]$ exit exit Script done, file is typescriptCopy the code
If we want to create a file name ourselves, or place the file somewhere else, we can simply follow the file name after the script.
[alvin@VM_0_16_centos test]$ script ~/alvin-script
Script started, file is /home/alvin/alvin-script
[alvin@VM_0_16_centos test]$ ll
total 64
-rw-rw-r-- 1 alvin alvin 21 Nov 10 09:40 test1.py
-rwxrwxr-x 1 alvin alvin 14074 Dec 31 07:35 test2
-rw-rw-r-- 1 alvin alvin 403 Dec 31 07:35 test2.cpp
-rw-rw-r-- 1 alvin alvin 2093 Nov 10 10:50 test2.py
-rwxrwxr-x 1 alvin alvin 8553 Jan 7 20:03 test3
-rw-rw-r-- 1 alvin alvin 78 Jan 7 20:03 test3.c
-rw-rw-r-- 1 alvin alvin 94 Nov 9 23:25 test.py
-rw-rw-r-- 1 alvin alvin 489 Jan 11 12:07 typescript
drwxrwxr-x 6 alvin alvin 4096 Nov 10 11:19 WeixinBot
drwxrwxr-x 6 alvin alvin 4096 Nov 10 11:30 wxpy
drwxrwxr-x 11 alvin alvin 4096 Nov 10 11:34 wxRobot
[alvin@VM_0_16_centos test]$ echo hello
hello
[alvin@VM_0_16_centos test]$ exit
exit
Script done, file is /home/alvin/alvin-script
Copy the code
By learning these two basic operations, you can cope with many scenarios in which terminals need to be recorded.
How to use Script to collaborate with colleagues?
Now I have a job that requires collaboration with a colleague. I do half of it and he does the other half.
First, I will do my work and record my working process in script:
[alvin@VM_0_16_centos test]$ script cooperate-job
Script started, file is cooperate-job
[alvin@VM_0_16_centos test]$ echo this is alvin_s job
this is alvin_s job
[alvin@VM_0_16_centos test]$ ls
cooperate-job test1.py test2 test2.cpp test2.py test3 test3.c test.py typescript WeixinBot wxpy wxRobot
[alvin@VM_0_16_centos test]$ exit
exit
Script done, file is cooperate-job
Copy the code
When you’re done, send a document to a colleague who can open it using a text tool to see how far you’ve come, and pick up where you left off.
If he continues to record his actions in your log file, add the -a option, short for Append.
[alvin@VM_0_16_centos test]$ script -a cooperate-job
Script started, file is cooperate-job
[alvin@VM_0_16_centos test]$ echo this is harry_s job
this is harry_s job
[alvin@VM_0_16_centos test]$ pwd
/home/alvin/test
[alvin@VM_0_16_centos test]$ exit
exit
Script done, file is cooperate-job
Copy the code
How to record the operation process of remote assistance?
Let others log on to their computer, if it is acquaintances well, is a stranger, the heart will be a little uneasy. To put our minds at ease, let’s secretly record what he’s doing.
A script command can be added to a Shell configuration file. Once the user logs in, the script command automatically starts and records all the actions of the operator.
To do this, we can modify the.bash_profile file.
vim ~/.bash_profile
Copy the code
On the last line, we add the script command:
/usr/bin/script -qa your_path # complete your own pathCopy the code
Then save it and use the source or. Command to make it work. The next time someone else logs on to the system, the script runs automatically and saves the record file in the location you specify.
In this case, the -q option represents silent recording, and the other party will not know that you are recording in the background. If this option is not used, he will receive this prompt:
Last login: Fri Jan 11 15:13:37 2019 from 119.33.28.6 Script started, File is /home/alvin/test/script-file # prompt [alvin@VM_0_16_centos ~]$Copy the code