{"id":13169,"date":"2019-04-01T04:02:03","date_gmt":"2019-04-01T04:02:03","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13169"},"modified":"2019-04-01T04:02:03","modified_gmt":"2019-04-01T04:02:03","slug":"how-to-start-linux-command-in-background-and-detach-process-in-terminal","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-start-linux-command-in-background-and-detach-process-in-terminal\/","title":{"rendered":"How to Start Linux Command in Background and Detach Process in Terminal"},"content":{"rendered":"<p>In this guide, we shall bring to light a simple yet important concept in\u00a0<a href=\"https:\/\/www.tecmint.com\/monitor-linux-processes-and-set-process-limits-per-user\/\" target=\"_blank\" rel=\"noopener\">process handling in a Linux system<\/a>, that is how to completely detach a process from its controlling terminal.<\/p>\n<p>When a process is associated with a terminal, two problems might occur:<\/p>\n<ol>\n<li>your controlling terminal is filled with so much output data and error\/diagnostic messages.<\/li>\n<li>in the event that the terminal is closed, the process together with its child processes will be terminated.<\/li>\n<\/ol>\n<p>To deal with these two issues, you need to totally detach a process from a controlling terminal. Before we actually move to solve the problem, let us briefly cover how to run processes in the background.<\/p>\n<h3>How to Start a Linux Process or Command in Background<\/h3>\n<p>If a process is already in execution, such as the\u00a0<a href=\"https:\/\/www.tecmint.com\/18-tar-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">tar command example<\/a>\u00a0below, simply press\u00a0<code>Ctrl+Z<\/code>\u00a0to stop it then enter the command\u00a0<code>bg<\/code>\u00a0to continue with its execution in the background as a job.<\/p>\n<p>You can view all your background jobs by typing\u00a0<code>jobs<\/code>. However, its\u00a0<strong>stdin<\/strong>,\u00a0<strong>stdout<\/strong>,\u00a0<strong>stderr<\/strong>\u00a0are still joined to the terminal.<\/p>\n<pre>$ tar -czf home.tar.gz .\r\n$ bg\r\n$ jobs\r\n<\/pre>\n<div id=\"attachment_23042\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Run-Linux-Command-in-Background.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23042\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Run-Linux-Command-in-Background.png\" alt=\"Run Linux Command in Background\" width=\"579\" height=\"211\" aria-describedby=\"caption-attachment-23042\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23042\" class=\"wp-caption-text\">Run Linux Command in Background<\/p>\n<\/div>\n<p>You can as well run a process directly from the background using the ampersand,\u00a0<code>&amp;<\/code>\u00a0sign.<\/p>\n<pre>$ tar -czf home.tar.gz . &amp;\r\n$ jobs\r\n<\/pre>\n<div id=\"attachment_23043\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Start-Linux-Process-in-Background.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23043\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Start-Linux-Process-in-Background.png\" alt=\"Start Linux Process in Background\" width=\"565\" height=\"135\" aria-describedby=\"caption-attachment-23043\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23043\" class=\"wp-caption-text\">Start Linux Process in Background<\/p>\n<\/div>\n<p>Take a look at the example below, although the\u00a0<a href=\"https:\/\/www.tecmint.com\/18-tar-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">tar command<\/a>\u00a0was started as a background job, an error message was still sent to the terminal meaning the process is still connected to the controlling terminal.<\/p>\n<pre>$ tar -czf home.tar.gz . &amp;\r\n$ jobs\r\n<\/pre>\n<div id=\"attachment_23044\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Linux-Process-Running-in-Background-Message.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23044\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Linux-Process-Running-in-Background-Message.png\" alt=\"Linux Process Running in Background Message\" width=\"693\" height=\"173\" aria-describedby=\"caption-attachment-23044\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23044\" class=\"wp-caption-text\">Linux Process Running in Background Message<\/p>\n<\/div>\n<h3>Keep Linux Processes Running After Exiting Terminal<\/h3>\n<p>We will use\u00a0<strong>disown<\/strong>\u00a0command, it is used after the a process has been launched and put in the background, it\u2019s work is to remove a shell job from the shell\u2019s active list jobs, therefore you will not use\u00a0<code>fg<\/code>,\u00a0<code>bg<\/code>\u00a0commands on that particular job anymore.<\/p>\n<p>In addition, when you close the controlling terminal, the job will not hang or send a\u00a0<strong>SIGHUP<\/strong>\u00a0to any child jobs.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/keep-remote-ssh-sessions-running-after-disconnection\/\" target=\"_blank\" rel=\"noopener\">5 Ways to Keep Remote SSH Sessions and Processes Running<\/a><\/p>\n<p>Let\u2019s take a look at the below example of using\u00a0<strong>diswon<\/strong>\u00a0bash built-in function.<\/p>\n<pre>$ sudo rsync Templates\/* \/var\/www\/html\/files\/ &amp;\r\n$ jobs\r\n$ disown  -h  %1\r\n$ jobs\r\n<\/pre>\n<div id=\"attachment_23046\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Keep-Linux-Processes-Running.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23046\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Keep-Linux-Processes-Running.png\" sizes=\"auto, (max-width: 771px) 100vw, 771px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Keep-Linux-Processes-Running.png 771w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Keep-Linux-Processes-Running-768x191.png 768w\" alt=\"Keep Linux Process Running After Closing Terminal\" width=\"771\" height=\"192\" aria-describedby=\"caption-attachment-23046\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23046\" class=\"wp-caption-text\">Keep Linux Process Running After Closing Terminal<\/p>\n<\/div>\n<p>You can also use\u00a0<code>nohup<\/code>\u00a0command, which also enables a process to continue running in the background when a user exits a shell.<\/p>\n<pre>$ nohup tar -czf iso.tar.gz Templates\/* &amp;\r\n$ jobs\r\n<\/pre>\n<div id=\"attachment_23047\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Put-Linux-Process-in-Background.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23047\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Put-Linux-Process-in-Background.png\" sizes=\"auto, (max-width: 803px) 100vw, 803px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Put-Linux-Process-in-Background.png 803w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/10\/Put-Linux-Process-in-Background-768x129.png 768w\" alt=\"Put Linux Process in Background After Closing Shell\" width=\"803\" height=\"135\" aria-describedby=\"caption-attachment-23047\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23047\" class=\"wp-caption-text\">Put Linux Process in Background After Closing Shell<\/p>\n<\/div>\n<h3>Detach a Linux Processes From Controlling Terminal<\/h3>\n<p>Therefore, to completely detach a process from a controlling terminal, use the command format below, this is more effective for graphical user interface (<strong>GUI<\/strong>) applications such as firefox:<\/p>\n<pre>$ firefox &lt;\/dev\/null &amp;&gt;\/dev\/null &amp;\r\n<\/pre>\n<p>In Linux,\u00a0<strong>\/dev\/null<\/strong>\u00a0is a special device file which writes-off (gets rid of) all data written to it, in the command above, input is read from, and output is sent to\u00a0<strong>\/dev\/null<\/strong>.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/screen-command-examples-to-manage-linux-terminals\/\" target=\"_blank\" rel=\"noopener\">10 Screen Command Examples to Detach Terminal Sessions<\/a><\/p>\n<p>As a concluding remark, provided a process is connected to a controlling terminal, as a user, you will see several output lines of the process data as well as error messages on your terminal. Again, when you close the a controlling terminal, your process and child processes will be terminated.<\/p>\n<p>Importantly, for any questions or remarks on the subject, reach us by using the comment form below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/run-linux-command-process-in-background-detach-process\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall bring to light a simple yet important concept in\u00a0process handling in a Linux system, that is how to completely detach a process from its controlling terminal. When a process is associated with a terminal, two problems might occur: your controlling terminal is filled with so much output data and error\/diagnostic &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-start-linux-command-in-background-and-detach-process-in-terminal\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Start Linux Command in Background and Detach Process in Terminal&#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-13169","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\/13169","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=13169"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13169\/revisions"}],"predecessor-version":[{"id":13170,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13169\/revisions\/13170"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}