19 November 2026 · One command a day
How do I keep a command running after I log out of Linux?
The nohup command starts a program so that it keeps running after you close your SSH session. Use it for a long job, such as a backup, a big import or a one-off script, that would otherwise stop the moment your connection drops.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
nohup ./backup.sh > backup.log 2>&1 &
Here is what each part does. nohup makes the program ignore the hangup signal that Linux sends when you log out. ./backup.sh is the command you want to run, and it can be any command with its own arguments. > backup.log sends normal output to a log file of your choice. 2>&1 sends error messages to the same file. Without the redirect, nohup writes everything to a file called nohup.out in the current folder. The & at the end runs the job in the background, so you get your prompt back straight away.
What you will see
$ nohup ./backup.sh > backup.log 2>&1 &
[1] 48213
$ ps -p 48213 -o pid,etime,cmd
PID ELAPSED CMD
48213 00:42 /bin/bash ./backup.sh
[1] is the job number in this shell and 48213 is the process ID. Write the process ID down, because you need it to check on the job or stop it later. The ps line confirms that the process is still alive and shows how long it has been running. You can now log out, log back in later, and the job will still be there. To see its progress, read the log with tail.
When to use it
- You are running a database import over SSH that will take an hour, and your home connection might drop.
- You want to start a script before you shut your laptop and check the result in the morning.
- You are testing an app by hand, for example
nohup python3 app.py > app.log 2>&1 &, before you set it up properly as a service.
Watch out for
nohup only protects the first command on the line. In nohup ./export.sh | gzip > out.gz & or nohup ./step1.sh && ./step2.sh, the second part is not covered and can stop when you log out. Wrap the whole thing in one shell instead: nohup bash -c './step1.sh && ./step2.sh' > run.log 2>&1 &. The same applies to sudo. Put sudo before the command that needs it, as in sudo nohup ./backup.sh > backup.log 2>&1 &, and note that the log file is created by your own shell, not by root.
Questions people ask
Does a nohup job survive a reboot?
No. nohup only protects a job from you logging out. To run a job at startup or on a schedule, use a systemd service, a process manager such as PM2, or a crontab entry.
How do I stop a command I started with nohup?
Run kill 48213 with the process ID it printed. If you have lost the ID, run pgrep -af backup.sh to find it.
Starting jobs that carry on after you leave is the kind of thing you can only do on a server you control. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.