{"id":1868,"date":"2017-03-23T17:11:09","date_gmt":"2017-03-23T11:41:09","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1868"},"modified":"2017-03-28T18:00:59","modified_gmt":"2017-03-28T12:30:59","slug":"can-close-terminal-without-killing-command-running","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/can-close-terminal-without-killing-command-running\/","title":{"rendered":"[Solved -7 Answers] LINUX &#8211; How can you close a terminal without killing the command running in it"},"content":{"rendered":"<p><label class=\"label label-warning\">PROBLEM:<\/label><\/p>\n<ul>\n<li>Sometimes you want to start a process and forget about it. If you start it from the command line, like this:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">redshift<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>you can&#8217;t close the terminal, or it will kill the process. Can you run a command in such a way that you can close the terminal without killing the process?<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<ul>\n<li>close a terminal without killing the command running in it<\/li>\n<li>One of the following 2 should work:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ nohup redshift &amp;<\/code><\/pre> <\/div>\n<p>OR<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ redshift &amp;<br\/>$ disown<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<ul>\n<li>If your program is already running you can pause it with Ctrl-Z, pull it into the background with bg and then disown it, like this:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ sleep 1000<br\/>^Z<br\/>[1]+  Stopped                 sleep 1000<br\/>$ bg<br\/>$ disown<br\/>$ exit<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<ul>\n<li>The reason that the process is killed on termination of the terminal is that the process you start is a child process of the terminal. Once you close the terminal, this will kill these child processes as well.<\/li>\n<li>You can see the process tree with pstree, for example when running kate &amp; in Konsole:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">init-+<br\/>     \u251c\u2500konsole\u2500\u252c\u2500bash\u2500\u252c\u2500kate\u2500\u2500\u25002*[{kate}]<br\/>     \u2502         \u2502      \u2514\u2500pstree<br\/>     \u2502         \u2514\u25002*[{konsole}]<\/code><\/pre> <\/div>\n<ul>\n<li>To make the kate process detached from konsole when you terminate konsole, use nohup with the command, like this:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">nohup kate &amp;<\/code><\/pre> <\/div>\n<ul>\n<li>After closing konsole, pstree will look like this:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">init-+ <br\/>|-kate---2*[{kate}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>and kate will survive.<\/li>\n<li>An alternative is using screen\/tmux\/byobu, which will keep the shell running, independent of the terminal.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<ul>\n<li>You can run the process like this in the terminal<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">setsid process<\/code><\/pre> <\/div>\n<ul>\n<li>This will run the program in a new session<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<ul>\n<li>use a variant on screen,the\u00a0 command\u00a0 like this<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">screen bash -c &#039;long_running_command_here; echo; read -p &quot;ALL DONE:&quot;&#039;<\/code><\/pre> <\/div>\n<ul>\n<li>The session can be disconnected with Ctrl A Ctrl D and reconnected in the simple case with screen -r. you have this wrapped in a script called session that lives in my PATH ready for convenient access:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">#!\/bin\/bash<br\/>#<br\/>if screen -ls | awk &#039;$1 ~ \/^[1-9][0-9]*\\.&#039;&quot;$1&quot;&#039;\/&#039; &gt;\/dev\/null<br\/>then<br\/>    echo &quot;WARNING: session is already running (reattach with &#039;screen -r $1&#039;)&quot; &gt;&amp;2<br\/>else<br\/>    exec screen -S &quot;$1&quot; bash -c &quot;$@; echo; echo &#039;--------------------&#039;; read -p &#039;ALL DONE (Enter to exit):&#039;&quot;<br\/>    echo &quot;ERROR: &#039;screen&#039; is not installed on this system&quot; &gt;&amp;2<br\/>fi<br\/>exit 1<\/code><\/pre> <\/div>\n<ul>\n<li>This only works when you know in advance you want to disconnect a program. It does not provide for an already running program to be disconnected.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<ul>\n<li>prefer:(applicationName &amp;)<\/li>\n<\/ul>\n<h4 id=\"example\"><span style=\"color: #000000;\"><strong>Example:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">           linux@linux-desktop:~$ (chromium-browser &amp;)<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>Make sure to use parenthesis when type the command!<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<ul>\n<li>You can set a process (PID) to not receive a HUP signal upon logging out and closing the terminal session.<\/li>\n<li>Use the following command:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BASH CODE<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">nohup -p PID<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM: Sometimes you want to start a process and forget about it. If you start it from the command line, like this: [ad type=&#8221;banner&#8221;] you can&#8217;t close the terminal, or it will kill the process. Can you run a command in such a way that you can close the terminal without killing the process? SOLUTION [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1699],"tags":[3944,3956,3957,3947,3950,3952,1591,3959,3949,3960,3946,3954,3945,3958,3953,3951,3948,3955],"class_list":["post-1868","post","type-post","status-publish","format-standard","hentry","category-linux","tag-application-in-terminal-still-running-but-not-directly-accessible","tag-difference-between-nohup","tag-disown-and","tag-even-if-terminal-is-closed","tag-how-can-i-access-a-vim-process-after-closing-its-terminal-on-macos","tag-how-can-i-close-a-terminal-without-killing-its-children-without-running-screen-first","tag-how-can-i-close-a-terminal-without-killing-the-command-running-in-it","tag-how-can-i-make-a-process-i-start-during-an-ssh-session-run-after-the-session-has-ended","tag-how-can-i-run-a-process-in-one-terminal-from-another-terminal","tag-how-to-keep-processes-running-after-ending-ssh-session","tag-how-to-make-sure-that-a-command-is-not-interrupted","tag-how-to-run-regular-programs-as-daemonsservices","tag-how-to-terminate-a-background-process","tag-i-am-using-why-isnt-the-process-running-in-the-background","tag-is-there-a-way-to-keep-a-session-alive-even-when-logging-out","tag-kill-an-unresponsive-ssh-session-without-closing-the-terminal","tag-run-multiple-commands-and-kill-them-as-one-in-bash","tag-submitting-non-interactive-jobs-over-ssh"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1868"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1868\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}