'script'및 'scriptreplay'명령을 사용하여 Linux 터미널 세션을 기록하고 재생하는 방법
이 가이드에서는 주어진 세션 동안 터미널에 인쇄 된 명령과 출력을 기록하는 데 도움이되는 Linux에서 스크립트 및 scriptreplay 명령을 사용하는 방법을 살펴볼 것입니다.

history 명령은 명령의 출력을 저장하지 않지만 사용자가 이전에 사용한 명령을 저장하는 데 도움이되는 훌륭한 명령 줄 유틸리티입니다.
따라서 스크립트 명령은 터미널에 인쇄 된 모든 것을 log_file에 기록하는 데 도움이되는 강력한 기능을 제공하는 데 유용합니다. 그런 다음 log_file에서 기록에있는 명령의 출력을 보려면 나중에이 파일을 참조 할 수 있습니다.
타이밍 정보를 사용하여 scriptreplay 명령을 사용하여 기록한 명령을 재생할 수도 있습니다.
스크립트 명령을 사용하여 Linux 터미널을 기록하는 방법
스크립트 명령은 사용자가 이름을 지정할 수있는 로그 파일에 터미널 활동을 저장합니다. 사용자가 이름을 제공하지 않은 경우 기본 파일 이름 인 typescript가 사용됩니다.
# script [options] - -timing=timing_file log_filename
Linux 터미널 기록을 시작하려면 다음과 같이 script를 입력하고 로그 파일 이름을 추가합니다.
[email protected] ~ $ script history_log.txt Script started, file is history_log.txt
스크립트를 중지하려면 exit를 입력하고 [Enter]를 누릅니다.
[email protected] ~ $ exit Script done, file is history_log.txt
스크립트가 명명 된 로그 파일에 쓸 수없는 경우 오류가 표시됩니다.
예를 들어, 아래 출력에서 파일 typescript의 권한은 사용자 나 그룹이 아닌 파일의 읽기, 쓰기 및 실행을 허용하지 않습니다. 로그 파일 이름없이 스크립트 명령을 실행하면 기본 파일에 쓰기를 시도하고 typescript에 오류가 표시됩니다.
[email protected] ~ $ ls -l typescript --------- 1 ubuntu ubuntu 144 Sep 15 00:00 typescript [email protected] ~ $ script script: open failed: typescript: Permission denied Terminated
아래 예에서 내 로그 파일의 이름을 script.log로 지정 했으므로 파일에 다른 이름을 지정할 수 있습니다.
[email protected] ~ $ script script.log
이제 스크립트가 터미널에서 실행 된 명령을 기록 할 수 있도록 몇 가지 명령을 실행 해보십시오.
[email protected] ~ $ cal September 2015 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [email protected] ~ $ w 14:49:40 up 4:06, 2 users, load average: 1.37, 1.56, 1.62 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT tecmint tty8 :0 10:45 4:06m 7:40 0.36s x-session-manager tecmint pts/5 :0 13:42 4.00s 0.07s 0.00s script script.log [email protected] ~ $ uptime 14:49:43 up 4:06, 2 users, load average: 1.37, 1.56, 1.62 [email protected] ~ $ whoami tecmint [email protected] ~ $ echo 'using script' using script [email protected] ~ $ exit exit Script done, file is script.log
이제 기록 된 모든 명령에 대한 로그 파일‘script.log‘를 확인하고 로그를 보면 스크립트가 줄 바꿈과 백 스페이스도 저장한다는 것을 알 수 있습니다.
[email protected] ~ $ vi script.log
^[[0m^[[255D^[[01;[email protected]^[[01;34m ~ $^[[00m cal^M September 2015 ^M Su Mo Tu We Th Fr Sa ^M 1 2 3 4 5 ^M 6 7 8 9 10 11 12 ^M 13 14 15 ^[[7m16^[[27m 17 18 19 ^M 20 21 22 23 24 25 26 ^M 27 28 29 30 ^M ^M ^[[01;[email protected]^[[01;34m ~ $^[[00m w^M 14:49:40 up 4:06, 2 users, load average: 1.37, 1.56, 1.62^M USER TTY FROM [email protected] IDLE JCPU PCPU WHAT^M tecmint tty8 :0 10:45 4:06m 7:40 0.36s x-session-manager^M tecmint pts/5 :0 13:42 4.00s 0.07s 0.00s script script.log^M ^[[01;[email protected]^[[01;34m ~ $^[[00m uptime^M 14:49:43 up 4:06, 2 users, load average: 1.37, 1.56, 1.62^M ^[[01;[email protected]^[[01;34m ~ $^[[00m whoami^M tecmint^M ^[[01;[email protected]^[[01;34m ~ $^[[00m echo ''^Hu'^Hs'^Hi'^Hn'^Hg'^H '^Hs'^Hc'^Hr'^Hi'^Hp'^Ht'^H^M using script^M ^[[01;[email protected]^[[01;34m ~ $^[[00m exit^M exit^M Script done on Wednesday 16 September 2015 02:49:59 PM IST ~
-a 옵션을 사용하여 이전 내용을 유지하면서 로그 파일이나 타이프 스크립트를 추가 할 수 있습니다.
[email protected] ~ $ script -a script.log Script started, file is script.log [email protected] ~ $ date Wed Sep 16 14:59:36 IST 2015 [email protected] ~ $ pwd /home/tecmint [email protected] ~ $ whereis script script: /usr/bin/script /usr/bin/X11/script /usr/share/man/man1/script.1.gz [email protected] ~ $ whatis script script (1) - make typescript of terminal session
스크립트의 내용을보고 추가하려면 -a 옵션을 사용한 후 기록하십시오.
[email protected] ~ $ vi script.log
^[[0m^[[255D^[[01;[email protected]^[[01;34m ~ $^[[00m date^M Wed Sep 16 14:59:36 IST 2015^M ^[[01;[email protected]^[[01;34m ~ $^[[00m pwd^M /home/tecmint^M ^[[01;[email protected]^[[01;34m ~ $^[[00m whre^H^[[K^H^[[Kereis script^M script: /usr/bin/script /usr/bin/X11/script /usr/share/man/man1/script.1.gz^M ^[[01;[email protected]^[[01;34m ~ $^[[00m whatis script^M script (1) - make typescript of terminal session^M ^[[01;[email protected]^[[01;34m ~ $^[[00m vi s^H^[[K^H^[[K^H^[[K^H^[[Kexit^M exit^M
대화식 쉘 세션이 아닌 단일 명령의 결과를 기록하려면 -c 옵션을 사용하십시오.
[email protected] ~ $ script -c 'hostname' script.log Script started, file is script.log tecmint.com Script done, file is script.log
스크립트를 자동 모드로 실행하려면 -q 옵션을 사용할 수 있습니다. 스크립트가 시작 또는 종료 중이라는 메시지가 표시되지 않습니다.
[email protected] ~ $ script -c 'who' -q script.log tecmint tty8 2015-09-16 10:45 (:0) tecmint pts/5 2015-09-16 13:42 (:0)
타이밍 정보를 표준 오류 또는 파일로 설정하려면 –timing 옵션을 사용하십시오. 타이밍 정보는 log_file에 저장된 출력을 다시 표시하려는 경우 유용합니다.
스크립트를 시작하고 기록 할 w, uptime 및 cal 명령을 실행 해 보겠습니다.
[email protected] ~ $ script --timing=time.txt script.log Script started, file is script.log [email protected] ~ $ w 15:09:31 up 4:26, 2 users, load average: 1.38, 1.39, 1.47 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT tecmint tty8 :0 10:45 4:26m 8:15 0.38s x-session-manager tecmint pts/5 :0 13:42 3.00s 0.09s 0.00s script --timing=time.txt script.log [email protected] ~ $ uptime 15:09:36 up 4:26, 2 users, load average: 1.43, 1.40, 1.48 [email protected] ~ $ cal September 2015 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
위의 타이밍 명령에 대한 script.log 및 time.txt 파일을 볼 수 있습니다.
[email protected] ~ $ vi script.log
^[[0m^[[255D^[[01;[email protected]^[[01;34m ~ $^[[00m w^M 15:12:05 up 4:28, 2 users, load average: 1.31, 1.37, 1.45^M USER TTY FROM [email protected] IDLE JCPU PCPU WHAT^M tecmint tty8 :0 10:45 4:28m 8:20 0.38s x-session-manager^M tecmint pts/5 :0 13:42 5.00s 0.09s 0.00s script --timing=time.txt script.log^M ^[[01;[email protected]^[[01;34m ~ $^[[00m uptime^M 15:12:07 up 4:28, 2 users, load average: 1.29, 1.36, 1.45^M ^[[01;[email protected]^[[01;34m ~ $^[[00m cal^M September 2015 ^M Su Mo Tu We Th Fr Sa ^M 1 2 3 4 5 ^M 6 7 8 9 10 11 12 ^M 13 14 15 ^[[7m16^[[27m 17 18 19 ^M 20 21 22 23 24 25 26 ^M 27 28 29 30 ^M ^M
이제 time.txt 파일을 봅니다.
[email protected] ~ $ vi time.txt
0.259669 306 0.037680 829 0.000006 2 0.000002 100 0.000002 2 0.000002 102 0.000019 202 0.000004 2 0.000002 102 0.000015 100 0.000002 2 0.000003 2 0.000002 99 0.000011 2 0.000003 82 ...
time.txt 파일에는 두 개의 열이 있습니다. 첫 번째 열은 마지막 표시 이후 경과 된 시간을 보여주고 두 번째 열은 이번에 표시된 문자 수를 보여줍니다.
매뉴얼 페이지 및 –help를 사용하여 스크립트 명령 줄 유틸리티 사용에 대한 추가 옵션 및 도움말을 찾으십시오.
스크립트 재생을 사용하여 타이밍 정보를 사용하여 스크립트 재생
scriptreplay 명령은 스크립트 명령으로 기록 된 log_file의 정보를 재생하는 데 도움이됩니다.
타이밍 정보는 스크립트 명령과 함께 사용되는 -timing = file 옵션으로 정의되며이 경우 파일은 스크립트 명령과 함께 사용 된 file.txt입니다.
스크립트 명령과 함께 사용한 log_file을 지정해야합니다.
이제 다음과 같이 실행 한 마지막 세 명령 w, uptime 및 cal을 재생 해 보겠습니다.
[email protected] ~ $ scriptreplay --timing=time.txt script.log

타이밍 정보를 사용하여 log_file을 재생하면 기록 된 명령이 실행되고 기록되는 동안 원래 출력이 표시되는 동시에 출력이 표시됩니다.
요약
이 두 명령, 스크립트 및 스크립트 재생은 사용하기 쉽고 동일한 명령 배치를 여러 번 실행해야 할 때 많은 도움이됩니다. 시스템과의 상호 작용을위한 명령 줄 인터페이스 만있는 서버를 관리하는 데 많은 도움이됩니다. 이 가이드가 도움이 되었기를 바라며, 추가 할 사항이 있거나이를 사용하는 동안 문제가 발생하면 주저하지 말고 댓글을 달아주세요.