Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to use nohup: Commands in Linux That Survive Terminal Closes

Published

Linux and Commands

You are running a backup script on your VPS. It takes two hours. You close your laptop and go to sleep. When you wake up, the backup is done. That is what nohup does.

What nohup Actually Does

Every Linux process runs inside a terminal session. When that terminal closes, the operating system sends a signal called SIGHUP (hangup) to all processes started from that session. That signal tells them the session is over and they should shut down.

nohup changes this behavior. It runs your command with SIGHUP blocked. When you close the terminal, your process keeps going. The operating system thinks it was started independently of any terminal, so it has no reason to send the hangup signal.

This is why nohup works for long-running tasks on a VPS. Your SSH connection is the terminal. When you disconnect, SIGHUP fires. But any process wrapped with nohup ignores it and continues running.

Your First nohup Command

The basic pattern looks like this:

nohup ./backup.sh &

The & at the end runs the command in the background. Combine it with nohup and you have a process that runs independently of your terminal session.

By default, nohup redirects output to a file called nohup.out. If your script prints anything, you will find it there instead of on your screen.

Running a Long File Transfer

Downloading a large file over an unstable connection is a common use case. If your SSH session drops mid-download, a regular wget dies with it. Wrap it in nohup and it outlives the connection:

nohup wget https://example.com/large-file.iso &

Check progress with ps aux | grep wget. The file lands in your current directory when it finishes.

Server Deployments That Take Time

Deployments often involve steps that take minutes, not seconds. Database migrations, Docker builds, large syncs. You do not want to stay connected through all of them.

nohup python deploy.py &

Add output redirection if you want to watch progress in a log file:

nohup python deploy.py > deploy.log 2>&1 &

The > deploy.log redirects standard output to a file you name yourself. The 2>&1 merges standard error into standard output so everything lands in the same log.

Exporting a Database

pg_dump is a classic example. A database export can easily take thirty minutes on a large dataset. You run it, close your laptop, and expect to find a ready SQL file hours later.

nohup pg_dump mydb > backup.sql 2>&1 &

That runs the export in the background, captures all output to backup.sql, and ignores any hangup signal. You can log out and come back later.

Keeping a Python Script Alive

Python scripts are everywhere on servers. If you start a long-running Python task over SSH and then close the connection, it dies. nohup keeps it alive:

nohup python3 train_model.py > training.log 2>&1 &

The script runs to completion. The output lands in training.log whether you are watching or not.

Finding Your Running Commands

After you log out and come back, how do you check if a nohup process is still running?

ps aux | grep backup

Look for your script name in the COMMAND column. If it shows up, it is still running. If it does not, it finished or crashed.

You can also look for the process ID:

pidof backup.sh

This prints the PID if the process exists.

Stopping What You Started

Sometimes a long-running process needs to stop before it finishes. Find the PID with ps or pidof, then kill it:

kill PID

If the process ignores the kill signal, try:

kill -9 PID

The -9 sends SIGKILL, which the process cannot ignore.

Common Mistakes to Avoid

Forgetting the ampersand is the most common error. Without &, the command runs in the foreground. Your terminal stays busy until the command finishes, which defeats the purpose.

Forgetting to redirect output is the second most common mistake. Without redirection, nohup still works, but you lose all output to a default file. If something goes wrong, you have no log to check. Always redirect to a named file:

nohup ./script.sh > script.log 2>&1 &

When screen Makes More Sense

nohup works well for fire-and-forget tasks. If you need to come back and interact with a running session, or you want to see live output scrolling by, screen is the better tool. screen creates a virtual terminal that you can reattach later. You see the same output you left behind.

For quick one-off tasks that just need to finish without you watching, nohup is simpler. For anything you might need to check on, interrupt, or peek at mid-run, reach for screen instead.

nohup in Practice

The next time you need to run something that takes longer than your attention span, nohup it. Database backups, large downloads, long builds. Log out, close the laptop, and trust that the task will finish without you holding the connection open.

Here is a quick reference for common use cases you will run into on a VPS:

# Download a serveriso and keep it running
nohup wget https://releases.ubuntu.com/24.04/ubuntu-24.04-live-server-amd64.iso > ubuntu-download.log 2>&1 &

# Run a Python scraper overnight
nohup python3 scrape_prices.py > scrape.log 2>&1 &

# Sync two directories without staying connected
nohup rsync -avz /home/user/data/ backup@remote:/backup/data/ > sync.log 2>&1 &

# Build a Docker image that takes forever
nohup docker build -t myapp . > docker-build.log 2>&1 &

Each of these runs in the background, ignores hangup signals, and writes everything to a log file you can check later. When you come back, just cat the log file and see what happened.

Related guides

Account services are unavailable until the production backend is configured.