{"id":105,"date":"2024-01-05T03:46:27","date_gmt":"2024-01-05T03:46:27","guid":{"rendered":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/?p=105"},"modified":"2024-01-05T04:00:37","modified_gmt":"2024-01-05T04:00:37","slug":"linux-screen","status":"publish","type":"post","link":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/?p=105","title":{"rendered":"Linux screen"},"content":{"rendered":"\n<p>Ah, the struggle of stepping away from the computer for a quick coffee break, only to return to a disconnected terminal.&nbsp;If you are frustrated by re-ssh-ing, navigating to the right folder, and scouring through command history, then you should learn about&nbsp;Linux command&nbsp;<code>screen<\/code>&nbsp; through the following exercises<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 1<\/h3>\n\n\n\n<p>Start a screen session, detach the session, and reattach it. It is assumed that you will complete these tasks after logging into a server.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Begin your session and give it a name by typing&nbsp;<code>screen -S TestSession<\/code>&nbsp;in the terminal. If a message appears, simply hit the \u201center\u201d key. Now, you have successfully entered a virtual session.<\/li>\n\n\n\n<li>Execute the&nbsp;<code>top<\/code>&nbsp;command to display all the active processes on the machine.<\/li>\n\n\n\n<li>Detach the screen by typing&nbsp;<code>ctrl a d<\/code>&nbsp;together (i.e. press and hold down the Ctrl key, then press A and continue to hold it down. Finally, press D).<br>You are now back in the terminal session where you started prior to launching screen.<\/li>\n\n\n\n<li>To reconnect to the \u201cTestSession\u201d screen session, simply enter&nbsp;<code>screen -R TestSession<\/code>&nbsp;in the terminal and you will find that the top command is still in operation, appearing as though the session was never terminated.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 2<\/h3>\n\n\n\n<p>Exit a screen session. Having completed the previous exercise, you are now aware that the TestSession session is running in the background on your computer. To learn how to end it correctly, or kill it, you should attempt this exercise.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Reattach to a session using&nbsp;<code>screen -R TestSession<\/code><\/li>\n\n\n\n<li>Exit the screen by typing&nbsp;<code>ctrl a k<\/code>&nbsp;together (i.e. press and hold down the Ctrl key, then press A and continue to hold it down. Finally, press K).<\/li>\n\n\n\n<li>Now if you try to reconnect again by entering&nbsp;<code>screen -R TestSession<\/code>&nbsp;you will be taken to a&nbsp;<em>new<\/em>&nbsp;session!<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 3<\/h3>\n\n\n\n<p>The full potential of screens is revealed when using multiple terminals within a single session. This is particularly useful when accessing a server and opening various folders or executing multiple commands while keeping the terminals open. After opening a new screen session or reattaching to an exiting one, try the following<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Type&nbsp;<code>ctrl a c<\/code>&nbsp;to open a new window (i.e.first press and hold the Ctrl key, then press A while still holding down Ctrl. Finally, press C.)<\/li>\n\n\n\n<li>If you repeat the same command again, it will open another window and take you there.<\/li>\n\n\n\n<li>To toggle to the next window (following a cyclic order), type&nbsp;<code>crtl a n<\/code>.<\/li>\n\n\n\n<li>If you want to go to the previous window (i.e. following a reverse cyclic order), type&nbsp;<code>ctrl a p<\/code>.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 4<\/h3>\n\n\n\n<p>Another great advantage of using screen is that you can use multiple screen sessions; unlike in the previous exercise, where you opened multiple terminals in one screen session. You can list all the opened sessions and reattach to any one of them.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open a session named \u201cTestSession1\u201d and detach from it.<\/li>\n\n\n\n<li>Open another session named \u201cTestSession2\u201d and detach from it.<\/li>\n\n\n\n<li>Now you are back to your the-real-terminal session. Type&nbsp;&nbsp;<code>screen -ls<\/code>&nbsp;to see the list of sessions that are active.<\/li>\n\n\n\n<li>Your output should look something like thisraghurama$ screen -ls There are screens on: 97253.TestSession2 (Detached) 97249.TestSession1 (Detached)<\/li>\n\n\n\n<li>If you want to connect (i.e. reattach) to one of the sessions, type&nbsp;<code>screen -S&nbsp;97249.TestSession1<\/code>, where I have also included the process ID (the number in front of the session name). You can also use the screen session name without the process ID,&nbsp;<code>screen -S TestSession1<\/code><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 5<\/h3>\n\n\n\n<p>One of the slightly inconvenient aspects of the screen command is that once your display is full, it doesn\u2019t allow direct scrolling using the mouse or up\/down arrows. However, there is a solution. Let\u2019s explore how to navigate the screen in this exercise.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open a session and print a lot of lines. For example, in a new screen session, type&nbsp;<code>seq 1 1000<\/code><\/li>\n\n\n\n<li>Now try to scroll up, and you will find that only previously used commands will be displayed.<\/li>\n\n\n\n<li>Type&nbsp;<code>ctrl a Esc<\/code>, and then you will be able to use the up\/down buttons to scroll the page up\/down. To escape from the scroll mode, you have to hit Esc again.<\/li>\n\n\n\n<li>Another option is to type&nbsp;<code>ctrl a [<\/code>&nbsp;which also enables scrolling. But now, one has to type&nbsp;<code>ctrl u\/d<\/code>&nbsp;for moving up\/down, and&nbsp;<code>ctrl f\/b<\/code>&nbsp;for page up\/down.<\/li>\n<\/ol>\n\n\n\n<p>Having the ability to scroll within a screen session proves to be very useful when reviewing the history of commands previously executed in that session.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ah, the struggle of stepping away from the computer for a quick coffee break, only to return to a disconnected terminal.&nbsp;If you are frustrated by re-ssh-ing, navigating to the right folder, and scouring through command history, then you should learn about&nbsp;Linux command&nbsp;screen&nbsp; through the following exercises Exercise 1 Start a screen session, detach the session, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"<!-- wp:paragraph -->\n<p>Ah, the struggle of stepping away from the computer for a quick coffee break, only to return to a disconnected terminal.&nbsp;If you are frustrated by re-ssh-ing, navigating to the right folder, and scouring through command history, then you should learn about&nbsp;Linux command&nbsp;<code>screen<\/code>&nbsp; through the following exercises<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:separator -->\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<!-- \/wp:separator -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3 class=\"wp-block-heading\">Exercise 1<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Start a screen session, detach the session, and reattach it. It is assumed that you will complete these tasks after logging into a server.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list {\"ordered\":true} -->\n<ol><!-- wp:list-item -->\n<li>Begin your session and give it a name by typing&nbsp;<code>screen -S TestSession<\/code>&nbsp;in the terminal. If a message appears, simply hit the \u201center\u201d key. Now, you have successfully entered a virtual session.<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Execute the&nbsp;<code>top<\/code>&nbsp;command to display all the active processes on the machine.<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Detach the screen by typing&nbsp;<code>ctrl a d<\/code>&nbsp;together (i.e. press and hold down the Ctrl key, then press A and continue to hold it down. Finally, press D).<br>You are now back in the terminal session where you started prior to launching screen.<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>To reconnect to the \u201cTestSession\u201d screen session, simply enter&nbsp;<code>screen -R TestSession<\/code>&nbsp;in the terminal and you will find that the top command is still in operation, appearing as though the session was never terminated.<\/li>\n<!-- \/wp:list-item --><\/ol>\n<!-- \/wp:list -->\n\n<!-- wp:separator -->\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<!-- \/wp:separator -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3 class=\"wp-block-heading\">Exercise 2<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Exit a screen session. Having completed the previous exercise, you are now aware that the TestSession session is running in the background on your computer. To learn how to end it correctly, or kill it, you should attempt this exercise.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list {\"ordered\":true} -->\n<ol><!-- wp:list-item -->\n<li>Reattach to a session using&nbsp;<code>screen -R TestSession<\/code><\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Exit the screen by typing&nbsp;<code>ctrl a k<\/code>&nbsp;together (i.e. press and hold down the Ctrl key, then press A and continue to hold it down. Finally, press K).<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Now if you try to reconnect again by entering&nbsp;<code>screen -R TestSession<\/code>&nbsp;you will be taken to a&nbsp;<em>new<\/em>&nbsp;session!<\/li>\n<!-- \/wp:list-item --><\/ol>\n<!-- \/wp:list -->\n\n<!-- wp:separator -->\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<!-- \/wp:separator -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3 class=\"wp-block-heading\">Exercise 3<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>The full potential of screens is revealed when using multiple terminals within a single session. This is particularly useful when accessing a server and opening various folders or executing multiple commands while keeping the terminals open. After opening a new screen session or reattaching to an exiting one, try the following<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list {\"ordered\":true} -->\n<ol><!-- wp:list-item -->\n<li>Type&nbsp;<code>ctrl a c<\/code>&nbsp;to open a new window (i.e.first press and hold the Ctrl key, then press A while still holding down Ctrl. Finally, press C.)<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>If you repeat the same command again, it will open another window and take you there.<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>To toggle to the next window (following a cyclic order), type&nbsp;<code>crtl a n<\/code>.<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>If you want to go to the previous window (i.e. following a reverse cyclic order), type&nbsp;<code>ctrl a p<\/code>.<\/li>\n<!-- \/wp:list-item --><\/ol>\n<!-- \/wp:list -->\n\n<!-- wp:separator -->\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<!-- \/wp:separator -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3 class=\"wp-block-heading\">Exercise 4<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Another great advantage of using screen is that you can use multiple screen sessions; unlike in the previous exercise, where you opened multiple terminals in one screen session. You can list all the opened sessions and reattach to any one of them.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list {\"ordered\":true} -->\n<ol><!-- wp:list-item -->\n<li>Open a session named \u201cTestSession1\u201d and detach from it.<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Open another session named \u201cTestSession2\u201d and detach from it.<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Now you are back to your the-real-terminal session. Type&nbsp;&nbsp;<code>screen -ls<\/code>&nbsp;to see the list of sessions that are active.<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Your output should look something like thisraghurama$ screen -ls There are screens on: 97253.TestSession2 (Detached) 97249.TestSession1 (Detached)<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>If you want to connect (i.e. reattach) to one of the sessions, type&nbsp;<code>screen -S&nbsp;97249.TestSession1<\/code>, where I have also included the process ID (the number in front of the session name). You can also use the screen session name without the process ID,&nbsp;<code>screen -S TestSession1<\/code><\/li>\n<!-- \/wp:list-item --><\/ol>\n<!-- \/wp:list -->\n\n<!-- wp:separator -->\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<!-- \/wp:separator -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3 class=\"wp-block-heading\">Exercise 5<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>One of the slightly inconvenient aspects of the screen command is that once your display is full, it doesn\u2019t allow direct scrolling using the mouse or up\/down arrows. However, there is a solution. Let\u2019s explore how to navigate the screen in this exercise.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list {\"ordered\":true} -->\n<ol><!-- wp:list-item -->\n<li>Open a session and print a lot of lines. For example, in a new screen session, type&nbsp;<code>seq 1 1000<\/code><\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Now try to scroll up, and you will find that only previously used commands will be displayed.<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Type&nbsp;<code>ctrl a Esc<\/code>, and then you will be able to use the up\/down buttons to scroll the page up\/down. To escape from the scroll mode, you have to hit Esc again.<\/li>\n<!-- \/wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Another option is to type&nbsp;<code>ctrl a [<\/code>&nbsp;which also enables scrolling. But now, one has to type&nbsp;<code>ctrl u\/d<\/code>&nbsp;for moving up\/down, and&nbsp;<code>ctrl f\/b<\/code>&nbsp;for page up\/down.<\/li>\n<!-- \/wp:list-item --><\/ol>\n<!-- \/wp:list -->\n\n<!-- wp:paragraph -->\n<p>Having the ability to scroll within a screen session proves to be very useful when reviewing the history of commands previously executed in that session.<\/p>\n<!-- \/wp:paragraph -->","_et_gb_content_width":"","footnotes":""},"categories":[4,6,5],"tags":[],"class_list":["post-105","post","type-post","status-publish","format-standard","hentry","category-computers","category-linux","category-programmerlife"],"_links":{"self":[{"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/posts\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=105"}],"version-history":[{"count":4,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":122,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions\/122"}],"wp:attachment":[{"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}