{"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[pastacode lang=\u201dbash\u201d manual=\u201dredshift%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>you can\u2019t 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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20nohup%20redshift%20%26%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>OR<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%24%20redshift%20%26%0A%24%20disown%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20sleep%201000%0A%5EZ%0A%5B1%5D%2B%20%20Stopped%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sleep%201000%0A%24%20bg%0A%24%20disown%0A%24%20exit%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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 & in Konsole:<\/li>\n<\/ul>\n[pastacode lang=\u201dbash\u201d manual=\u201dinit-%2B%0A%20%20%20%20%20%E2%94%9C%E2%94%80konsole%E2%94%80%E2%94%AC%E2%94%80bash%E2%94%80%E2%94%AC%E2%94%80kate%E2%94%80%E2%94%80%E2%94%802*%5B%7Bkate%7D%5D%0A%20%20%20%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%E2%94%82%20%20%20%20%20%20%E2%94%94%E2%94%80pstree%0A%20%20%20%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%E2%94%94%E2%94%802*%5B%7Bkonsole%7D%5D%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dnohup%20kate%20%26%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>After closing konsole, pstree will look like this:<\/li>\n<\/ul>\n[pastacode lang=\u201dbash\u201d manual=\u201dinit-%2B%20%0A%7C-kate\u20142*%5B%7Bkate%7D%0A%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dbash\u201d manual=\u201dsetsid%20process%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dscreen%20bash%20-c%20\u2019long_running_command_here%3B%20echo%3B%20read%20-p%20%22ALL%20DONE%3A%22\u2019%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%23!%2Fbin%2Fbash%0A%23%0Aif%20screen%20-ls%20%7C%20awk%20\u2019%241%20~%20%2F%5E%5B1-9%5D%5B0-9%5D*%5C.\u2019%22%241%22\u2019%2F\u2019%20%3E%2Fdev%2Fnull%0Athen%0A%20%20%20%20echo%20%22WARNING%3A%20session%20is%20already%20running%20(reattach%20with%20\u2019screen%20-r%20%241\u2032)%22%20%3E%262%0Aelse%0A%20%20%20%20exec%20screen%20-S%20%22%241%22%20bash%20-c%20%22%24%40%3B%20echo%3B%20echo%20\u2032\u2014\u2014\u2014\u2014\u2014\u2014\u2013\u2018%3B%20read%20-p%20\u2019ALL%20DONE%20(Enter%20to%20exit)%3A\u2019%22%0A%20%20%20%20echo%20%22ERROR%3A%20\u2019screen\u2019%20is%20not%20installed%20on%20this%20system%22%20%3E%262%0Afi%0Aexit%201%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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 &)<\/li>\n<\/ul>\n<h4 id=\"example\"><span style=\"color: #000000;\"><strong>Example:<\/strong><\/span><\/h4>\n[pastacode lang=\u201dbash\u201d manual=\u201d%20%20%20%20%20%20%20%20%20%20%20linux%40linux-desktop%3A~%24%20(chromium-browser%20%26)%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dbash\u201d manual=\u201dnohup%20-p%20PID%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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: [pastacode lang=\u201dbash\u201d manual=\u201dredshift%0A\u201d message=\u201dBASH CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [ad type=\u201dbanner\u201d] you can\u2019t close the terminal, or it will kill the process. Can you run a command in such a way that you can close [&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}]}}