nohup and &

linux
bash
Author

Deven Mistry

Published

March 20, 2024

Yesterday, I ran into a problem where I wanted to run a shell script in the background on a linux machine. I knew that

bash foo.sh &

would solve the problem for me, however I was working on a remote machine, which meant that closing the ssh connection would terminate the execution of the command.

Enter nohup. This is a wonderful bash utility which allows you to run your script in the background on a remote machine even when you close the connection.

nohup is just short for no hang up. You can use this command in multiple ways. There’s a wonderful article on DigitalOcean1 explaining this command. I’ll just list a few of them here.

By default, nohup will save the output of the execution in nohup.out. You can change that with the redirect output command.


nohup ./foo.sh
nohup ./foo.sh > output.txt     # write (redirect) output to output.txt
nohup ./foo.sh & > output.txt   # write (redirect) output to output.txt and run the script in the background

Here’s another good reference from StackOverFlow2