Today I will be showing you how to use the Linux nohup command. What the nohup command does is lets you run a script or command without fear of it exiting if you close your session while it is still running. For example if you have a spotty WIFI connection and need to execute an automatic script that will take a few minutes to finish, rather than risking it exiting if you lose your connection, you can use the nohup command and it will continue executing even if you get disconnected.
Nohup Command Examples
Here is an example of how you can use the nohup command, note that you will still see the output and be able to interact as if you weren’t using nohup.
You can combine the example with other tricks such as running as a background process or even using bash redirection to redirect the output into a file. Here are two more examples of nohup using these options.
Start script or command using nohup as background process and put output into log file:
nohup "~/scripts/script.sh" > ~/scriptlogs/log.txt &
Start script or command using nohup and put output into log file:
nohup "~/scripts/script.sh" > ~/scriptlogs/log.txt
Start script or command using nohup as background process:
nohup "~/scripts/script.sh" &
One important thing to keep in mind with the nohup command is that if you get disconnected your shell will exit and your new session’s shell will not be able to bring it back to the foreground. You will not be able to interact with said script or command anymore, this also applies even if you start your nohup command as a background process (this is because the process is not a child of your new shell).
I hope this guide on Linux nohup command was helpful, please don’t forget to show some love by liking, commenting, or sharing using the social media buttons below. Thank you!