Skip to content

Run a Linux Command in the Background with nohup and disown

Updated 6 min read

Updated: 11 October 2026 · Applies to: bash 5.2 and GNU nohup 9.4 (Ubuntu 24.04), bash 5.3 and uutils nohup 0.10.0 (Ubuntu 26.04) and systemd 259, tested

To run a Linux command in the background, put & at the end: sleep 300 &. The shell starts it and gives you the prompt back at once. To keep the command running after you log out or your SSH connection drops, start it with nohup and send its output to a file:

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

Already started a long command in the foreground? Press Ctrl+Z, type bg to let it continue in the background, then disown -h %1 so it survives the logout.

Quick reference

command &                          # run in the background
jobs -l                            # list background jobs with their PIDs
Ctrl+Z, then bg                    # move a running command to the background
fg %1                              # bring job 1 back to the foreground
kill %1                            # stop job 1
nohup command > out.log 2>&1 &     # keep running after logout
disown -h %1                       # protect a job that is already running

Do not mix up & and &&: one & starts the command in the background, while cmd1 && cmd2 runs cmd2 only after cmd1 succeeded.

1. Run a command in the background with &

The shell prints the job number in brackets and the process ID (PID). Lab output from Ubuntu 26.04:

$ sleep 300 &
[1] 130
$ jobs
[1]+  Running                    sleep 300 &

echo $! prints the PID of the last background command, which is handy in scripts. A background job still writes to your terminal and still belongs to your session, so send its output to a file: tar -czf etc.tar.gz /etc > tar.log 2>&1 &.

2. Move a running command to the background (Ctrl+Z and bg)

  1. Press Ctrl+Z. The command is paused, not stopped for good:
    $ sleep 400
    ^Z
    [2]+  Stopped                    sleep 400
  2. Type bg to let it continue in the background:
    $ bg
    [2]+ sleep 400 &
    $ jobs -l
    [1]-   130 Running                    sleep 300 &
    [2]+   135 Running                    sleep 400 &
  3. To bring it back, type fg (the job marked +) or fg %2.

3. What is nohup?

nohup ("no hang up") starts a command that ignores the hangup signal (SIGHUP), which a shell sends to its jobs when the terminal closes or the SSH connection drops. nohup does not put the command in the background by itself: add &.

$ nohup sleep 500 &
[3] 144
$ nohup: ignoring input and appending output to 'nohup.out'

When the output still points at the terminal, nohup writes it to nohup.out in the current folder (created with mode 0600). If that folder is not writable, it uses your home folder instead; in our test as a normal user in /: nohup: ignoring input and appending output to '/home/alice/nohup.out'.

Choose the file yourself, so logs from different jobs don’t end up in one nohup.out. These are the messages we got with each redirect (GNU nohup on Ubuntu 24.04; Ubuntu 26.04 printed the same first message):

  • nohup cmd > out.log & prints nohup: ignoring input and redirecting stderr to stdout, so errors also go to out.log.
  • nohup cmd > out.log 2>&1 & prints nothing on the screen; the line nohup: ignoring input goes into out.log.
  • nohup cmd < /dev/null > out.log 2>&1 & prints no message at all. This is the cleanest form for scripts.

4. disown: protect a job that is already running

disown is a bash command for jobs you started without nohup. disown %1 removes job 1 from the shell’s job list, so the shell will not send it a hangup. disown -h %1 keeps it in the list (you can still use fg) but marks it so it gets no hangup either. Plain disown acts on the current job (the one marked +).

5. What survives a logout: our test

We started background jobs in an interactive bash on Ubuntu 26.04 and then sent the shell a hangup, which is what happens when you close the terminal window or the SSH connection drops:

  • sleep 300 & and a job moved with Ctrl+Z and bg: killed.
  • nohup sleep 500 &: kept running.
  • A job after disown and one after disown -h: kept running.
  • setsid sleep 504 (runs the command in a new session): kept running.

Typing exit is different: bash’s huponexit option is off by default, and in our test even a plain sleep 911 & survived a normal exit. Do not rely on that, because a dropped connection kills such jobs. With paused jobs, bash refuses the first exit with There are stopped jobs.; resume them with bg or end them first.

On servers with systemd, logind can also end all your processes at logout, nohup jobs included, if KillUserProcesses=yes is set in /etc/systemd/logind.conf. According to the logind.conf manual the default is no; Ubuntu 26.04 ships the line commented out as #KillUserProcesses=no.

6. Find and stop background processes

jobs -l               # jobs of this shell, with PIDs
pgrep -a sleep        # find a process by name (also after you logged out)
kill %1               # end job 1 of this shell
kill 353              # end a process by PID
wait                  # wait until all background jobs of this shell have finished

After you log in again, jobs is empty: jobs belong to the shell that started them. Use pgrep -a or ps aux to find nohup processes, and kill PID to stop them. A job ended with kill %1 shows [1]+ Terminated sleep 600 at the next prompt.

7. Better tools for long jobs

  • To watch the job again later, use a terminal multiplexer: start it inside tmux (see how to install and use tmux) or the screen command, detach, log out, and reattach later to see the live output.
  • To run it as a one-off systemd job (as root), use systemd-run. The job runs outside your login session and its output goes to the journal. Lab output on Ubuntu 26.04 (date and host name in the journal lines shortened to ...):
    $ sudo systemd-run --unit=etc-backup tar -czf /root/etc-backup.tar.gz /etc
    Running as unit: etc-backup.service; invocation ID: ab293499b63f4d03a48d7da4e440c16f
    $ sudo journalctl -u etc-backup
    ... systemd[1]: Started etc-backup.service - [systemd-run] /usr/bin/tar -czf /root/etc-backup.tar.gz /etc.
    ... tar[369]: /usr/bin/tar: Removing leading `/' from member names
    ... systemd[1]: etc-backup.service: Deactivated successfully.
    Check a running job with systemctl status etc-backup and end it with systemctl stop etc-backup.
  • For something that must always run (a bot, a game server, a worker), create a systemd service that restarts it and starts at boot: see run a custom script as a systemd service.

For a quick task, nohup ... & is enough; for anything you need to check on, use tmux; for anything permanent, use a service. If you need a hand setting up long-running jobs on your VPS or dedicated server, our Hire an Expert engineers can help.

Official documentation: GNU nohup, bash job control builtins and logind.conf.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket