{"id":12919,"date":"2019-03-29T02:50:39","date_gmt":"2019-03-29T02:50:39","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12919"},"modified":"2019-03-29T02:50:39","modified_gmt":"2019-03-29T02:50:39","slug":"how-to-record-and-replay-linux-terminal-sessions-using-script-and-scriptreplay-commands","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/29\/how-to-record-and-replay-linux-terminal-sessions-using-script-and-scriptreplay-commands\/","title":{"rendered":"How to Record and Replay Linux Terminal Sessions using \u2018script\u2019 and \u2018scriptreplay\u2019 Commands"},"content":{"rendered":"<p>In this guide we are going to look at how to use a\u00a0<strong>script<\/strong>\u00a0and\u00a0<strong>scriptreplay<\/strong>\u00a0commands in Linux that can help you to record commands and their output printed on your terminal during a given session.<\/p>\n<div id=\"attachment_15789\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Record-Linux-Terminal-Commands.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-15789\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Record-Linux-Terminal-Commands.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Record-Linux-Terminal-Commands.png 715w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Record-Linux-Terminal-Commands-620x298.png 620w\" alt=\"Record Linux Terminal Commands\" width=\"620\" height=\"298\" aria-describedby=\"caption-attachment-15789\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15789\" class=\"wp-caption-text\">Record and Replay Linux Terminal Commands<\/p>\n<\/div>\n<p>The\u00a0<a href=\"https:\/\/www.tecmint.com\/history-command-examples\/\" target=\"_blank\" rel=\"noopener\">history command<\/a>\u00a0is a great command-line utility that helps users to store previous command used, though it does not store the output of a command.<\/p>\n<p><b>Don\u2019t Miss:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/showterm-io-a-terminalshell-recording-upload-and-share-tool-for-linux\/\" target=\"_blank\" rel=\"noopener\">Showterm.io \u2013 A Linux Terminal Recording Tool<\/a><\/p>\n<p><b>Don\u2019t Miss:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/best-linux-screen-recorders-for-desktop-screen-recording\/\" target=\"_blank\" rel=\"noopener\">8 Best Desktop Screen Recorders for Linux<\/a><\/p>\n<p>Therefore the\u00a0<strong>script<\/strong>\u00a0command comes in handy to provide you a powerful functionality that helps you to record everything that is printed on your terminal to a\u00a0<strong>log_file<\/strong>. You can then refer to this file later on in case you want to view the output of a command in history from the\u00a0<strong>log_file<\/strong>.<\/p>\n<p>You can also replay commands that you recorded using the\u00a0<strong>scriptreplay<\/strong>\u00a0command by using a timing information.<\/p>\n<h3>How to Record Linux Terminal Using script Command<\/h3>\n<p>The\u00a0<strong>script<\/strong>\u00a0command stores terminal activities in a log file that can be named by a user, when a name is not provided by a user, the default file name,\u00a0<strong>typescript<\/strong>\u00a0is used.<\/p>\n<h6>Basic Syntax of script Command<\/h6>\n<pre># script [options] - -timing=timing_file log_filename\r\n<\/pre>\n<p>To start recording of Linux terminal, type\u00a0<strong>script<\/strong>\u00a0and add the log\u00a0<strong>filename<\/strong>\u00a0as shown.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>script history_log.txt<\/strong>\r\n\r\n<strong>Script started, file is history_log.txt<\/strong>\r\n<\/pre>\n<p>To stop\u00a0<strong>script<\/strong>, type\u00a0<strong>exit<\/strong>\u00a0and press\u00a0<strong>[Enter]<\/strong>.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>exit<\/strong>\r\n\r\n<strong>Script done, file is history_log.txt<\/strong>\r\n<\/pre>\n<p>If the script can not write to the named log file then it shows an error.<\/p>\n<p>For example, in the output below, the permissions of the file\u00a0<strong>typescript<\/strong>\u00a0does not allow reading, writing and execution of the file not by any user or group. When you run the script command without a log file name, it attempts to write to the default file,\u00a0<strong>typescript<\/strong>\u00a0hence showing an error.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>ls -l typescript<\/strong>\r\n\r\n<strong>--------- 1 ubuntu ubuntu 144 Sep 15 00:00 typescript<\/strong>\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>script<\/strong>\r\n\r\n<strong>script: open failed: typescript: Permission denied<\/strong>\r\n<strong>Terminated<\/strong>\r\n<\/pre>\n<h4>Examples of using the script command<\/h4>\n<p>I have named my log file\u00a0<strong>script.log<\/strong>\u00a0in the example below, you can give your file a different name.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>script script.log<\/strong>\r\n<\/pre>\n<p>Now try to execute few commands to allow script to record executed commands on the terminal.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>cal<\/strong>\r\n\r\n   September 2015     \r\nSu Mo Tu We Th Fr Sa  \r\n       1  2  3  4  5  \r\n 6  7  8  9 10 11 12  \r\n13 14 15 16 17 18 19  \r\n20 21 22 23 24 25 26  \r\n27 28 29 30           \r\n                      \r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>w<\/strong>\r\n\r\n 14:49:40 up  4:06,  2 users,  load average: 1.37, 1.56, 1.62\r\nUSER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT\r\ntecmint  tty8     :0               10:45    4:06m  7:40   0.36s x-session-manager\r\ntecmint  pts\/5    :0               13:42    4.00s  0.07s  0.00s script script.log\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>uptime<\/strong>\r\n\r\n 14:49:43 up  4:06,  2 users,  load average: 1.37, 1.56, 1.62\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>whoami<\/strong>\r\n\r\ntecmint\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>echo 'using script'<\/strong>\r\n\r\nusing script\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>exit<\/strong>\r\nexit\r\nScript done, file is script.log\r\n<\/pre>\n<p>Now try to view the log file \u2018<strong>script.log<\/strong>\u2018 for all recorded commands, while you view the log you realize that the script also stores line feeds and backspaces.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>vi script.log<\/strong>\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>^[[0m^[[255D^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m cal^M\r\n   September 2015     ^M\r\nSu Mo Tu We Th Fr Sa  ^M\r\n       1  2  3  4  5  ^M\r\n 6  7  8  9 10 11 12  ^M\r\n13 14 15 ^[[7m16^[[27m 17 18 19  ^M\r\n20 21 22 23 24 25 26  ^M\r\n27 28 29 30           ^M\r\n                      ^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m w^M\r\n 14:49:40 up  4:06,  2 users,  load average: 1.37, 1.56, 1.62^M\r\nUSER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT^M\r\ntecmint  tty8     :0               10:45    4:06m  7:40   0.36s x-session-manager^M\r\ntecmint  pts\/5    :0               13:42    4.00s  0.07s  0.00s script script.log^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m uptime^M\r\n 14:49:43 up  4:06,  2 users,  load average: 1.37, 1.56, 1.62^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m whoami^M\r\ntecmint^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m echo ''^Hu'^Hs'^Hi'^Hn'^Hg'^H '^Hs'^Hc'^Hr'^Hi'^Hp'^Ht'^H^M\r\nusing script^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m exit^M\r\nexit^M\r\n\r\nScript done on Wednesday 16 September 2015 02:49:59 PM IST\r\n~                                                              \r\n<\/pre>\n<p>You may use the\u00a0<strong>-a<\/strong>\u00a0option to append the log file or typescript, retaining the prior contents.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>script -a script.log<\/strong>\r\nScript started, file is script.log\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>date<\/strong>\r\nWed Sep 16 14:59:36 IST 2015\r\n\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>pwd<\/strong>\r\n\/home\/tecmint\r\n\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>whereis script<\/strong>\r\nscript: \/usr\/bin\/script \/usr\/bin\/X11\/script \/usr\/share\/man\/man1\/script.1.gz\r\n\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>whatis script<\/strong>\r\nscript (1)           - make typescript of terminal session\r\n<\/pre>\n<p>View the contents of script, log after using\u00a0<strong>-a<\/strong>\u00a0option to append it.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>vi script.log<\/strong>\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>^[[0m^[[255D^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m date^M\r\nWed Sep 16 14:59:36 IST 2015^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m pwd^M\r\n\/home\/tecmint^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m whre^H^[[K^H^[[Kereis script^M\r\nscript: \/usr\/bin\/script \/usr\/bin\/X11\/script \/usr\/share\/man\/man1\/script.1.gz^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m whatis script^M\r\nscript (1)           - make typescript of terminal session^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m vi s^H^[[K^H^[[K^H^[[K^H^[[Kexit^M\r\nexit^M\r\n<\/pre>\n<p>To log results of a single command other than an interactive shell session, use the\u00a0<strong>-c<\/strong>\u00a0option.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>script -c 'hostname' script.log<\/strong>\r\n\r\nScript started, file is script.log\r\ntecmint.com\r\nScript done, file is script.log<\/pre>\n<p>If you want script to run in a quiet mode then you can use the\u00a0<strong>-q<\/strong>\u00a0option. You will not see a message that shows script is starting or exiting.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>script -c 'who'  -q  script.log<\/strong>\r\n\r\ntecmint  tty8         2015-09-16 10:45 (:0)\r\ntecmint  pts\/5        2015-09-16 13:42 (:0)\r\n<\/pre>\n<p>To set timing information to standard error or a file use the\u00a0<strong>\u2013timing<\/strong>\u00a0option. The timing information is useful when you want to re-display the output stored in the log_file.<\/p>\n<p>Let us start script and run the following commands\u00a0<strong>w<\/strong>,\u00a0<strong>uptime<\/strong>\u00a0and\u00a0<strong>cal<\/strong>\u00a0to be recorded.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>script --timing=time.txt script.log<\/strong>\r\nScript started, file is script.log\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>w<\/strong>\r\n 15:09:31 up  4:26,  2 users,  load average: 1.38, 1.39, 1.47\r\nUSER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT\r\ntecmint  tty8     :0               10:45    4:26m  8:15   0.38s x-session-manager\r\ntecmint  pts\/5    :0               13:42    3.00s  0.09s  0.00s script --timing=time.txt script.log\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>uptime<\/strong>\r\n 15:09:36 up  4:26,  2 users,  load average: 1.43, 1.40, 1.48\r\n\r\n<strong>tecmint@tecmint ~ $<\/strong> <strong>cal<\/strong>\r\n   September 2015     \r\nSu Mo Tu We Th Fr Sa  \r\n       1  2  3  4  5  \r\n 6  7  8  9 10 11 12  \r\n13 14 15 16 17 18 19  \r\n20 21 22 23 24 25 26  \r\n27 28 29 30    \r\n<\/pre>\n<p>You can view the\u00a0<strong>script.log<\/strong>\u00a0and\u00a0<strong>time.txt<\/strong>\u00a0file for the timing command above.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>vi script.log<\/strong>\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>^[[0m^[[255D^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m w^M\r\n 15:12:05 up  4:28,  2 users,  load average: 1.31, 1.37, 1.45^M\r\nUSER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT^M\r\ntecmint  tty8     :0               10:45    4:28m  8:20   0.38s x-session-manager^M\r\ntecmint  pts\/5    :0               13:42    5.00s  0.09s  0.00s script --timing=time.txt script.log^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m uptime^M\r\n 15:12:07 up  4:28,  2 users,  load average: 1.29, 1.36, 1.45^M\r\n^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m cal^M\r\n   September 2015     ^M\r\nSu Mo Tu We Th Fr Sa  ^M\r\n       1  2  3  4  5  ^M\r\n 6  7  8  9 10 11 12  ^M\r\n13 14 15 ^[[7m16^[[27m 17 18 19  ^M\r\n20 21 22 23 24 25 26  ^M\r\n27 28 29 30           ^M\r\n                      ^M\r\n\r\n<\/pre>\n<p>Now view\u00a0<strong>time.txt<\/strong>\u00a0file.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>vi time.txt<\/strong>\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>0.259669 306\r\n0.037680 829\r\n0.000006 2\r\n0.000002 100\r\n0.000002 2\r\n0.000002 102\r\n0.000019 202\r\n0.000004 2\r\n0.000002 102\r\n0.000015 100\r\n0.000002 2\r\n0.000003 2\r\n0.000002 99\r\n0.000011 2\r\n0.000003 82\r\n...\r\n<\/pre>\n<p>The\u00a0<strong>time.txt<\/strong>\u00a0file has two columns, the first column shows how much time has elapsed since the last display and the second column, shows the number of characters that have been displayed this time around.<\/p>\n<p>Use the man page and\u00a0<strong>\u2013help<\/strong>\u00a0to seek for more options and help in using the script command-line utility.<\/p>\n<h3>Using scriptreplay to replay scripts using timing information<\/h3>\n<p>The\u00a0<strong>scriptreplay<\/strong>\u00a0command helps to replay information in your\u00a0<strong>log_file<\/strong>\u00a0recorded by the\u00a0<strong>script<\/strong>\u00a0command.<\/p>\n<p>The timing information is defined by the\u00a0<strong>-timing=file<\/strong>\u00a0option used with the\u00a0<strong>script<\/strong>\u00a0command and\u00a0<strong>file<\/strong>\u00a0in this case is\u00a0<strong>file.txt<\/strong>\u00a0that was used with script command .<\/p>\n<p>Remember you need to specify the\u00a0<strong>log_file<\/strong>\u00a0you used with the script command.<\/p>\n<p>Let us now replay the last three commands\u00a0<strong>w<\/strong>,\u00a0<strong>uptime<\/strong>\u00a0and\u00a0<strong>cal<\/strong>\u00a0that we had run as follows.<\/p>\n<pre><strong>tecmint@tecmint ~ $<\/strong> <strong>scriptreplay --timing=time.txt script.log<\/strong>\r\n<\/pre>\n<div id=\"attachment_15785\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Check-Last-Executed-Commands.gif\"><img decoding=\"async\" class=\"size-medium wp-image-15785\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Check-Last-Executed-Commands.gif\" alt=\"Replay Last Executed Commands in Linux\" aria-describedby=\"caption-attachment-15785\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15785\" class=\"wp-caption-text\">Replay Last Executed Commands in Linux<\/p>\n<\/div>\n<p>When the\u00a0<strong>log_file<\/strong>\u00a0replayed using the timing information, the commands recorded are run and their output is displayed at the same time the original output was displayed while being recorded.<\/p>\n<h3>Summary<\/h3>\n<p>These two commands,\u00a0<strong>script<\/strong>\u00a0and\u00a0<strong>scriptreplay<\/strong>\u00a0easy to use and help a lot when you need to run the same batch of commands several times. They help a lot in managing servers that have only command-line interface for interaction with your system. Hope this guide was useful and if you have anything to add or face a challenge while using them, do not hesitate to post a comment.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/record-and-replay-linux-terminal-session-commands-using-script\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide we are going to look at how to use a\u00a0script\u00a0and\u00a0scriptreplay\u00a0commands in Linux that can help you to record commands and their output printed on your terminal during a given session. Record and Replay Linux Terminal Commands The\u00a0history command\u00a0is a great command-line utility that helps users to store previous command used, though it &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/29\/how-to-record-and-replay-linux-terminal-sessions-using-script-and-scriptreplay-commands\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Record and Replay Linux Terminal Sessions using \u2018script\u2019 and \u2018scriptreplay\u2019 Commands&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12919","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12919"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12919\/revisions"}],"predecessor-version":[{"id":12920,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12919\/revisions\/12920"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}