HomeLinuxHow to Keep Shell Commands Running After Closing the Terminal?

How to Keep Shell Commands Running After Closing the Terminal?

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 commands continue to run, whether you’re managing long-running tasks, continuous services, or you need to disconnect from a server.

Linux offers a suite of commands like nohup, screen, disown, at, and tmux 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:

CommandUse CaseLimitation
nohup1. Running long-running processes that you don’t need to interact with after starting.
2. Basic background execution without the need for script or command interaction.
1. Does not allow you to reattach to the session.
2. Not suitable for interactive applications.
screen1. Managing multiple command-line sessions from a single terminal.
2. Detaching and reattaching to terminal sessions, useful for long-running processes, especially over SSH.
1. Slightly more complex to use compared to nohup.
2. Not as modern or feature-rich as tmux.
disown1. Similar to nohup, but used for processes already started.
2. Useful for disassociating running jobs from the current shell without ending them.
1. Specific to bash shell.
2. Does not allow reattaching to the session.
tmux1. Handling multiple terminal sessions within a single window.
2. Detaching and reattaching to sessions, with a more user-friendly interface and advanced features compared to screen.
3. Useful for long-running jobs, collaborative work, and session management.
1. Requires learning its command syntax and shortcuts.
2. Might be overkill for simple background job management.

Using nohup Command

The “No Hang UP” command, or nohup, is an essential tool for any Linux user’s toolbox. It permits a command to carry on in the background, guaranteeing the uninterrupted operation of your procedure.

nohup command &

Replace command with your desired command. For example, to run a Bash script in the background:

nohup ./script.sh &

By default, output is redirected to nohup.out. To change this:

nohup command > outputfile.txt 2>&1 &

Utilizing screen Command

The screen command is ideal for preserving processes after disconnecting since it offers detachable shell sessions. Install it on your machine first:

sudo apt-get install screen


sudo yum install screen

After successful installation of the screen utility, Start screen, run your command, and then detach using Ctrl-A then Ctrl-D. To reattach:

screen -r [session-id]

Leveraging tmux Command

Similar to screen, tmux offers robust terminal session management. Install it as follows:

sudo apt-get install tmux


sudo yum install tmux

Start tmux with tmux, run your command, and detach with Ctrl-B then D. To reattach:

tmux attach-session -t [session-name]

Scheduling with at Command

The at command schedules command for future execution, independent of your terminal session. Install it using:

sudo apt-get install at


sudo yum install at

After installing at utility, use it with the following commands:

echo "command" | at [time]

Replace command with your desired command. For example, to run a Python script in the background for the next 2 minutes:

echo "python script.py" | at now + 2 minutes

Using disown Command

The “disown” 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.

Start a job (command &) and then use disown to remove it from the job table.

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.

Summary: Best Use Cases

Simple Background Execution: nohup
Basic Session Persistence: screen
Disassociating Current Shell Jobs: disown
Advanced Session Management and Features: tmux

Scroll to Top