Skip to main content

Running Linux command in a background with SSH client disconnected

This post explores various methods to keep commands running independently of your SSH session.

Updated this week

Running Linux commands in the background while keeping them active even after disconnecting from an SSH session is crucial for long-running processes. Without proper handling, an interrupted connection can terminate important tasks, causing delays and data loss.

nohup

nohup (no hang-up) allows a command to continue running after logout. It is installed by default on most Linux distributions and mainly used for running scripts or commands that need to persist after logout.

Examples

nohup myscript.sh & - Runs a script in the background, output saved to nohup.out.

nohup python3 script.py & - Runs a Python script persistently.

nohup bash -c 'long_running_command' & - Executes a long-running command safely.

Bring the command back to foreground

Use ps aux | grep myscript.sh to find the process ID.

Attach to the process using fg %<job_number> if it was a background job.

Summarize

nohup is simple and effective for keeping processes running after logout but lacks session management.

Screen

screen is a terminal multiplexer allowing session persistence, however unlike nohup is not installed by default on all distributions. Install using:

sudo apt install screen (Debian/Ubuntu)
sudo yum install screen (RHEL/CentOS).

Ideal for managing multiple terminal sessions and long-running processes.

Examples

screen -S mysession - Starts a named screen session.

Inside screen: mycommand - Runs a command inside the session. The command could be a script as well. It will create an instance inside the screen's terminal, which is run on the machine.

Detach: Ctrl + A, then D - Detaches the session.

Bring the command back to foreground

List sessions with screen -ls.

Reattach with screen -r mysession - "mysession" is a session name you have named.

Summarize

screen is powerful for session persistence and interactive processes but lacks modern enhancements.

tmux

tmux is a modern alternative to screen, offering better usability and scripting capabilities. Is not always installed. To install it run:

sudo apt install tmux (Debian/Ubuntu)
sudo yum install tmux (RHEL/CentOS).

Used for managing multiple terminal sessions and improving workflow efficiency.

Examples

tmux - Starts a new session.

Inside tmux: mycommand - Runs a command or a script.

Detach with Ctrl + B, then D.

Bring the command back to foreground

List sessions with tmux list-sessions.

Reattach with tmux attach-session -t 0 (replace 0 with session ID).

Summarize

tmux offers session persistence, improved usability over screen, and powerful scripting features.

disown

disown removes jobs from the shell’s job table to prevent termination on logout. Comes preinstalled in most Linux distributions as part of Bash. Best for keeping background jobs alive without additional tools.

Examples

mycommand & - Start a process in the background.

jobs - List background jobs.

disown -h %1 - Keep job 1 running after logout.

Bring the command back to foreground
Find the process ID with ps aux | grep mycommand.

Use kill -CONT <PID> to resume a stopped process.

Summarize

disown is simple and effective for keeping jobs alive but lacks advanced management features.

systemd

systemd is a system and service manager used for persistent and restartable services. Preinstalled on most modern Linux distributions and is ideal for managing long-running services and background tasks.

Examples

1. Create a service file: /etc/systemd/system/myscript.service


2. Add:
[Unit]
Description=My Persistent Script
[Service]
ExecStart=/path/to/myscript.sh
Restart=always
[Install]
WantedBy=multi-user.target


3. Enable and start: systemctl enable myscript && systemctl start myscript. Myscript is your script's name.

Bring the command back to foreground

Check status with systemctl status myscript
View logs with journalctl -u myscript -f

Summarize

systemd is the best choice for persistent and restartable services, offering robustness but requiring configuration.

Did this answer your question?