{"id":6559,"date":"2014-05-06T19:35:16","date_gmt":"2014-05-06T19:35:16","guid":{"rendered":"https:\/\/noi3.org\/site\/?p=6559"},"modified":"2014-05-06T19:35:16","modified_gmt":"2014-05-06T19:35:16","slug":"how-to-detach-a-linux-process-from-shell-ssh-terminal-etc","status":"publish","type":"post","link":"https:\/\/site.noi3.org\/?p=6559","title":{"rendered":"How to detach a Linux process from shell (ssh, terminal, etc)"},"content":{"rendered":"<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \t<img decoding=\"async\" src=\"https:\/\/encrypted-tbn0.gstatic.com\/images?q=tbn:ANd9GcS3y04LoN2SSy90eaEYrk2zVkL-gk5bwKs4dQQJw5TzLzoTK7mN\" \/><\/p>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \tWhen you execute a Linux command resulting in a background process, the process will be killed automatically if a controlling terminal is closed, or a ssh session is terminated. It is because a shell, which is the parent process of all background processes running in the terminal\/ssh session, sends a SIGINT to all its child processes. If you want a process to keep running after a terminal session is over, that is when\u00a0<strong style=\"border: 0px; font-family: inherit; font-size: 15px; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;\">detaching a Linux process from shell<\/strong>\u00a0can help.<\/p>\n<p>  <!--more-->  <\/p>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \t<span style=\"font-size: 15px; line-height: 22px;\">When you execute a Linux command resulting in a background process, the process will be killed automatically if a controlling terminal is closed, or a ssh session is terminated. It is because a shell, which is the parent process of all background processes running in the terminal\/ssh session, sends a SIGINT to all its child processes. If you want a process to keep running after a terminal session is over, that is when\u00a0<\/span><strong style=\"font-family: inherit; font-size: 15px; line-height: 22px; font-style: inherit; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;\">detaching a Linux process from shell<\/strong><span style=\"font-size: 15px; line-height: 22px;\">\u00a0can help.<\/span><\/p>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \tHere is how you can detach a process from bash shell.<\/p>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \tIf a given process is running in the foreground, press Ctrl+z to interrupt it.<\/p>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \tThen run it in the background.<\/p>\n<div class=\"console\" style=\"border: 0px none; font-family: monospace; font-size: 13px; margin: 0px 0px 15px; outline: 0px; padding: 5px; vertical-align: baseline; color: rgb(255, 255, 255); background-color: rgb(0, 153, 204); overflow: auto; white-space: nowrap;\"> \t$ bg<\/div>\n<pre style=\"border: 1px dashed rgb(204, 204, 204); background-color: rgb(238, 238, 238); padding: 10px; margin-top: 0px; margin-bottom: 15px; overflow: auto; color: rgb(68, 82, 99);\">                     [1]+ my_command &amp;                     <\/pre>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \tFinally, type\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">disown<\/tt>\u00a0along with job sequence number of the backgrounded job.<\/p>\n<div class=\"console\" style=\"border: 0px none; font-family: monospace; font-size: 13px; margin: 0px 0px 15px; outline: 0px; padding: 5px; vertical-align: baseline; color: rgb(255, 255, 255); background-color: rgb(0, 153, 204); overflow: auto; white-space: nowrap;\"> \t$ disown %1<\/div>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \t<tt style=\"font-size: 15px; border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">disown<\/tt><span style=\"font-size: 15px; line-height: 22px;\">\u00a0is a built-in shell command that causes a shell to not send SIGHUP to disowned child processes, thereby allowing them to continue even after the shell process is terminated. Once a background process is disowned by a shell, the process will disappear from a job list when you type\u00a0<\/span><tt style=\"font-size: 15px; border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">jobs<\/tt><span style=\"font-size: 15px; line-height: 22px;\">\u00a0command, which means that you can safely close a terminal or log out without killing the process.<\/span><\/p>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \tWhen run without any argument,\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">disown<\/tt>\u00a0removes the most recent job from a job list.<\/p>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \tNot all shells support\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">disown<\/tt>, unfortunately.\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">disown<\/tt>\u00a0is supported in\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">bash<\/tt>\u00a0and\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">zsh<\/tt>, but shells like\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">csh<\/tt>,\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">tcsh<\/tt>,\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">dash<\/tt>\u00a0do not support it.<\/p>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \tBesides\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">disown<\/tt>, you can also use\u00a0<tt style=\"border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 15px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">nohup<\/tt>\u00a0to detach a Linux process from shell, but in this case, at the time of process launch.<\/p>\n<div class=\"console\" style=\"border: 0px none; font-family: monospace; font-size: 13px; margin: 0px 0px 15px; outline: 0px; padding: 5px; vertical-align: baseline; color: rgb(255, 255, 255); background-color: rgb(0, 153, 204); overflow: auto; white-space: nowrap;\"> \t$ nohup &lt;my_command&gt; &amp;<\/div>\n<p style=\"border: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 15px; margin: 0px 0px 0.9em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(68, 82, 99); line-height: 22px;\"> \t<span style=\"font-size: 15px; line-height: 22px;\">While\u00a0<\/span><tt style=\"font-size: 15px; border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">disown<\/tt><span style=\"font-size: 15px; line-height: 22px;\">\u00a0prevents a closing shell from sending SIGUP signals,\u00a0<\/span><tt style=\"font-size: 15px; border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">nohup<\/tt><span style=\"font-size: 15px; line-height: 22px;\">\u00a0configures a command to ignore SIGHUP signals sent by the shell. So once any command is launched with\u00a0<\/span><tt style=\"font-size: 15px; border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">nohup<\/tt><span style=\"font-size: 15px; line-height: 22px;\">, it does not matter whether or not the shell sends out SIGHUP. Any\u00a0<\/span><tt style=\"font-size: 15px; border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;\">nohup<\/tt><span style=\"font-size: 15px; line-height: 22px;\">-ed command will continue to run.<\/span><\/p>\n<hr \/>\n<p> \t<a href=\"http:\/\/xmodulo.com\/2013\/04\/how-to-detach-linux-process-from-shell.html\">Articolul original<\/a><\/p>\n<hr \/>\n<p> \t\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you execute a Linux command resulting in a background process, the process will be killed automatically if a controlling terminal is closed, or a&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[1315,969,312,1314,1316],"class_list":["post-6559","post","type-post","status-publish","format-standard","hentry","category-informatica","tag-background","tag-comanda","tag-linux","tag-shell","tag-spate"],"_links":{"self":[{"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/posts\/6559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/site.noi3.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6559"}],"version-history":[{"count":0,"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/posts\/6559\/revisions"}],"wp:attachment":[{"href":"https:\/\/site.noi3.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/site.noi3.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/site.noi3.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}