{"id":2331,"date":"2024-01-09T21:06:03","date_gmt":"2024-01-09T15:36:03","guid":{"rendered":"https:\/\/www.veeble.org\/kb\/?p=2331"},"modified":"2025-03-12T10:42:04","modified_gmt":"2025-03-12T05:12:04","slug":"keep-shell-commands-running-guide","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/keep-shell-commands-running-guide\/","title":{"rendered":"How to Run and Keep Linux Command in Background?"},"content":{"rendered":"\n<p>Have you ever needed to keep a shell command running after logging out or closing the terminal? This tutorial will walk you through numerous ways to make sure your run linux command in background, whether you&#8217;re managing long-running tasks, continuous services, or you need to disconnect from a server.<\/p>\n\n\n\n<p>Linux offers a suite of commands like <a href=\"https:\/\/www.veeble.org\/kb\/what-is-nohup-command-and-how-to-use-it\/\" target=\"_blank\" rel=\"noopener\">nohup<\/a>, <a href=\"https:\/\/www.veeble.org\/kb\/screen-command-in-linux-usage-and-examples\/\" target=\"_blank\" rel=\"noopener\">screen<\/a>, <a href=\"https:\/\/docs.oracle.com\/cd\/E86824_01\/html\/E54763\/disown-1.html\" target=\"_blank\" rel=\"noopener\">disown<\/a>, <a href=\"https:\/\/www.veeble.org\/kb\/scheduling-one-time-jobs-in-linux-using-at-command\/\" target=\"_blank\" rel=\"noopener\">at<\/a>, and <a href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/tmux.1.html\" target=\"_blank\" rel=\"noopener\">tmux<\/a> each designed for specific process and session management tasks. These commands vary in functionality, from simple background tasks to advanced session control. The right choice depends on the task complexity and the required level of process interaction. Understanding these commands is key to effective and efficient management of tasks in a Linux environment. Please have a look at the below table to have an idea before doing those commands:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Use Case<\/th><th>Limitation<\/th><\/tr><\/thead><tbody><tr><td>nohup<\/td><td>1. Running long-running processes that you don&#8217;t need to interact with after starting.<br>2. Basic background execution without the need for script or command interaction.<\/td><td>1. Does not allow you to reattach to the session.<br>2. Not suitable for interactive applications.<\/td><\/tr><tr><td>screen<\/td><td>1. Managing multiple command-line sessions from a single terminal.<br>2. Detaching and reattaching to terminal sessions, useful for long-running processes, especially over SSH.<\/td><td>1. Slightly more complex to use compared to nohup.<br>2. Not as modern or feature-rich as tmux.<\/td><\/tr><tr><td>disown<\/td><td>1. Similar to nohup, but used for processes already started.<br>2. Useful for disassociating running jobs from the current shell without ending them.<\/td><td>1. Specific to bash shell.<br>2. Does not allow reattaching to the session.<\/td><\/tr><tr><td>tmux<\/td><td>1. Handling multiple terminal sessions within a single window.<br>2. Detaching and reattaching to sessions, with a more user-friendly interface and advanced features compared to <code>screen<\/code>.<br>3. Useful for long-running jobs, collaborative work, and session management.<\/td><td>1. Requires learning its command syntax and shortcuts.<br>2. Might be overkill for simple background job management.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"using-nohup-command\">Using nohup Command<\/h4>\n\n\n<p>The &#8220;No Hang UP&#8221; command, or <a href=\"https:\/\/www.veeble.org\/kb\/what-is-nohup-command-and-how-to-use-it\/\" target=\"_blank\" rel=\"noopener\">nohup<\/a>, is an essential tool for any Linux user&#8217;s toolbox. It permits a command to carry on in the background, guaranteeing the uninterrupted operation of your procedure.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nohup command &amp;<\/code><\/pre>\n\n\n\n<p>Replace <code>command<\/code> with your desired command. For example, to run a Bash script in the background:<\/p>\n\n\n\n<p>Confused about the <code>&amp;<\/code> symbol? Refer to: <a href=\"https:\/\/www.veeble.org\/kb\/run-linux-command-in-the-background-using-and-command\/\" target=\"_blank\" rel=\"noopener\">Run Linux Command in the Background Using <code>&amp;<\/code><\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nohup .\/script.sh &amp;<\/code><\/pre>\n\n\n\n<p>By default, output is redirected to <code>nohup.out<\/code>. To change this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nohup command &gt; outputfile.txt 2&gt;&amp;1 &amp;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"utilizing-screen-command\">Utilizing screen Command<\/h4>\n\n\n<p>The <a href=\"https:\/\/linux.die.net\/man\/1\/screen\" target=\"_blank\" rel=\"noopener\">screen command<\/a> is ideal for preserving processes after disconnecting since it offers detachable shell sessions. Install it on your machine first:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>sudo apt-get install screen<\/code><\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\">OR<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>sudo yum install screen<\/code><\/code><\/pre>\n\n\n\n<p>After successful installation of the screen utility, Start <code>screen<\/code>, run your command, and then detach using <code>Ctrl-A<\/code> then <code>Ctrl-D<\/code>. To reattach:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>screen -r &#91;session-id]<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"leveraging-tmux-command\">Leveraging tmux Command<\/h4>\n\n\n<p>Similar to screen, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/tmux.1.html\" target=\"_blank\" rel=\"noopener\">tmux <\/a>offers robust terminal session management. Install it as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>sudo apt-get install tmux<\/code><\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\">OR<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>sudo yum install tmux<\/code><\/code><\/pre>\n\n\n\n<p>Start tmux with <code>tmux<\/code>, run your command, and detach with <code>Ctrl-B<\/code> then <code>D<\/code>. To reattach:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tmux attach-session -t &#91;session-name]<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"scheduling-with-at-command\">Scheduling with at Command<\/h4>\n\n\n<p>The <a href=\"https:\/\/www.veeble.org\/kb\/scheduling-one-time-jobs-in-linux-using-at-command\/\" target=\"_blank\" rel=\"noopener\">at command<\/a> schedules command for future execution, independent of your terminal session. Install it using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>sudo apt-get install at<\/code><\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\">OR<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>sudo yum install at<\/code><\/code><\/pre>\n\n\n\n<p>After installing at utility, use it with the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"command\" | at &#91;time]<\/code><\/pre>\n\n\n\n<p>Replace <code>command<\/code> with your desired command. For example, to run a Python script in the background for the next 2 minutes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"python script.py\" | at now + 2 minutes<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"using-disown-command\">Using disown Command<\/h4>\n\n\n<p>The &#8220;<a href=\"https:\/\/docs.oracle.com\/cd\/E86824_01\/html\/E54763\/disown-1.html\" target=\"_blank\" rel=\"noopener\">disown<\/a>&#8221; command allows a job to remain in the background by removing it from the job table. You can quickly make sure your process never stops by using this built-in command.<\/p>\n\n\n\n<p>Start a job (<code>command &amp;<\/code>) and then use <code>disown<\/code> to remove it from the job table.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Each method described above offers unique features for keeping shell commands running after terminal exit. Experiment with these techniques to find which best suits your needs.<\/p>\n\n\n\n<div class=\"wp-block-uagb-separator uagb-block-6fc08f95\"><div class=\"wp-block-uagb-separator__inner\" style=\"--my-background-image:\"><\/div><\/div>\n\n\n\n<div class=\"wp-block-uagb-info-box uagb-block-27e1a521 uagb-infobox__content-wrap  uagb-infobox-icon-above-title uagb-infobox-image-valign-top\"><div class=\"uagb-ifb-content\"><div class=\"uagb-ifb-icon-wrap\"><svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M0 256C0 114.6 114.6 0 256 0C397.4 0 512 114.6 512 256C512 397.4 397.4 512 256 512C114.6 512 0 397.4 0 256zM371.8 211.8C382.7 200.9 382.7 183.1 371.8 172.2C360.9 161.3 343.1 161.3 332.2 172.2L224 280.4L179.8 236.2C168.9 225.3 151.1 225.3 140.2 236.2C129.3 247.1 129.3 264.9 140.2 275.8L204.2 339.8C215.1 350.7 232.9 350.7 243.8 339.8L371.8 211.8z\"><\/path><\/svg><\/div><div class=\"uagb-ifb-title-wrap\"><h3 class=\"uagb-ifb-title\">Summary: Best Use Cases<\/h3><\/div><p class=\"uagb-ifb-desc\"><strong>Simple Background Execution<\/strong>: nohup<br><strong>Basic Session Persistence<\/strong>: screen<br><strong>Disassociating Current Shell Jobs<\/strong>: disown<br><strong>Advanced Session Management and Features<\/strong>: tmux<\/p><\/div><\/div>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-3cf77f4a wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Reliable cPanel Hosting<\/h3><p class=\"uagb-cta__desc\">Secure, dependable, and easy-to-use cPanel hosting. Perfect for your website.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/cpanel-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">Choose Your Plan<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>Related Blogs:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-veeble-hosting wp-block-embed-veeble-hosting\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/www.veeble.org\/kb\/what-is-nohup-command-and-how-to-use-it\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-veeble-hosting wp-block-embed-veeble-hosting\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/www.veeble.org\/kb\/screen-command-in-linux-usage-and-examples\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Discover expert techniques to run and keep linux command in background after closing the terminal with our step to step instructions.<\/p>\n","protected":false},"author":7,"featured_media":8009,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2331","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Run-and-Keep-Linux-Command-in-Background.jpg",1366,768,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Run-and-Keep-Linux-Command-in-Background-150x150.jpg",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Run-and-Keep-Linux-Command-in-Background-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Run-and-Keep-Linux-Command-in-Background-768x432.jpg",768,432,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Run-and-Keep-Linux-Command-in-Background-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Run-and-Keep-Linux-Command-in-Background.jpg",1366,768,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Run-and-Keep-Linux-Command-in-Background.jpg",1366,768,false]},"uagb_author_info":{"display_name":"Adrian Antony","author_link":"https:\/\/www.veeble.com\/kb\/author\/adrian\/"},"uagb_comment_info":0,"uagb_excerpt":"Discover expert techniques to run and keep linux command in background after closing the terminal with our step to step instructions.","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=2331"}],"version-history":[{"count":22,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2331\/revisions"}],"predecessor-version":[{"id":7595,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2331\/revisions\/7595"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/8009"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=2331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=2331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=2331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}