Skip to content
Log In Sign Up info@muvcloud.com

Run a Command with nohup

nohup ./backup.sh &

When an SSH session ends, the shell sends the hangup signal (SIGHUP) to the jobs it started, and most programs stop when they receive it. nohup starts the command with that signal ignored. The trailing & puts the job in the background, so you get your prompt back and can log out.

The general form is nohup COMMAND [ARGUMENTS]. Anything that runs from the shell works: a script, a program, or a command with options, such as nohup rsync -a /var/www/ /backup/www/ &.

Where the Output Goes

If the output would go to your terminal, nohup appends it to a file called nohup.out in the current folder instead, and prints a short notice saying so. If it cannot write there, it uses nohup.out in your home folder. Error output follows standard output into the same file.

Over weeks, nohup.out files pile up in random folders and can grow large. Name the log file yourself instead.

Redirect Output to Your Own Log

nohup ./backup.sh > ~/backup.log 2>&1 &
RedirectionResult
> job.log 2>&1Output and errors in one file, replaced each run.
>> job.log 2>&1Same, but appended.
> out.log 2> err.logOutput and errors in separate files.
> /dev/null 2>&1Output discarded.

The order matters: 2>&1 must come after the file redirection. If the program might wait for keyboard input, add < /dev/null so it does not stop in the background.

Check That the Job Is Still Running

echo $!
ps -p 12345 -o pid,etime,cmd
pgrep -af backup.sh
tail -f ~/backup.log

Right after you start the job, $! holds its process ID. Replace 12345 with that number. After you log in again, pgrep -af finds the job by name. More ways to find a process are in how to list running processes in Linux.

To stop the job, send the normal termination signal with kill 12345 and give it a few seconds to exit cleanly. Use kill -9 only if it ignores that.

Forgot nohup? Use disown

If a long command is already running in the foreground, you can still detach it from the shell:

  1. Press Ctrl+Z to pause it.
  2. Run bg to continue it in the background.
  3. Run jobs -l to see its job number.
  4. Run disown -h %1 so the shell does not send it the hangup signal.

disown is a bash built-in. The job's output still points at your terminal; once you log out, that output is lost, and a program that fails when it cannot write may stop.

When nohup Is Not Enough

  • A reboot: every process ends. A job that must start again after a reboot belongs in a systemd service.
  • Logout cleanup by systemd: if KillUserProcesses=yes is set in /etc/systemd/logind.conf, all processes of a session end at logout, with or without nohup. The upstream default is yes, but Debian and Ubuntu ship it set to no; check the file on other distributions.
  • sudo with nohup: a password prompt in the background stops the job. Run sudo -v first so sudo does not ask again.

A one-off job that must survive logout cleanup can run as a temporary service: sudo systemd-run --unit=backup-job /home/deploy/backup.sh, with its output in journalctl -u backup-job. Such a temporary service is not started again after a reboot.

tmux or a systemd Service Instead

If you want to come back and watch the job, use tmux. Install it with sudo apt install tmux on Debian and Ubuntu or sudo dnf install tmux on AlmaLinux and Rocky Linux:

tmux new -s work
# start the command, then press Ctrl+B and then D to detach
tmux attach -t work

For a program that must run all the time, such as an application server or a queue worker, write a service file: see systemd unit files explained. systemd restarts it after a crash, starts it at boot, and keeps its logs.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations