In Linux, the nohup command is a powerful tool that lets you run tasks in the background even after you log out. It makes sure that your processes run even when you aren’t there to watch over them. When you use nohup to run a process, it is disconnected from the terminal and keeps running in the background.
Contents
Description About nohup command
This command works by redirecting the standard output and error streams of the process to a file named nohup.out. In addition to its use for long-running processes, it is also useful when dealing with processes that produce a large amount of output.
The nohup command is also useful for running processes on remote servers, where the user may not have an active terminal session for the entire duration of the process.
Syntax of nohup command in Linux
The syntax of this command is as follows:
nohup [command] [arguments] &
- It is the command to run a process in the background, even after you log out of the system.
- [command] is the command you want to run.
- [arguments] are any optional arguments you want to pass to the command.
- The & symbol at the end of the command lets it run in the background.

For example, to run the sleep command for 10 seconds in the background, you would use the following command:
nohup sleep 10 &
This would run the sleep command in the background and keep it running. The output of the command would be redirected to a log file named nohup.out in the current directory.
Running a Command or Shell-Script Even after Logout using nohup
Follow the below steps to run a command even after logout:
- Open the terminal.
- Enter the command you want to run.
- Precede the command with nohup
- For example nohup command-name
- End the command line with &.
- This ensures that the command runs in the background.
- Press Enter.
- Log out of the terminal.
- The command continues to run even after you log out.
Note:
The output of the command is stored in a file named nohup.out by default. You can use this command with a shell script in the same way. To stop this command, log in to the terminal, and use kill followed by the process ID.
Checking the status of a Process
- Open a terminal window
- To open a terminal window in Linux, you can use the keyboard shortcut Ctrl + Alt + T or search for “Terminal” in your application launcher.
- To check the status of the process, use the ps command and grep for the process name or PID. For example ps -ef | grep [process name/PID].
- You can verify that the process is running by checking the log file: nohup.out
- You can view the contents of the log file by using the following command: cat nohup.out
Nohup options in Linux
The Linux nohup command has the following options:
- -f: Forces nohup to overwrite the log file if it already exists.
- -p: This option allows you to specify a different PID for the process.
- —: This option is used to specify the end of options for nohup, and any arguments after this option are passed as arguments to the command being run.
In general, the most commonly used option for this command is &, which allows the process to run in the background. The other options are used less frequently and depend on the specific requirements of your use case.
Running Multiple Commands using Nohup
Start the first process
Type the following command in the terminal: nohup sleep 60 &
- The sleep 60 command tells the system to wait for 60 seconds.
- The & symbol at the end of the command lets it run in the background.
- This command tells the system to keep running the process even if you close the terminal window.
Start the second process
Type the following command in the terminal:
nohup sleep 120 &
- The sleep 120 command tells the system to wait for 120 seconds.
- The & symbol at the end of the command lets it run in the background.
Verifying the Processes
- Verify that both processes are running by checking their log files: nohup.out.
- You can view the contents of a specific log file by using the following command: cat nohup.out
- You can now log out of the system or close the terminal window, and the processes you started with this command continue running.
More from us:
Conclusion
Finally, the nohup command in Linux is a must-have for running background processes, particularly long-running processes. It keeps the process from being stopped and it redirects the standard output and error streams to a file for further inspection. It is a handy tool for users who need to run processes remotely or who need to run processes that produce a big amount of output due to its ability to keep programs running in the background.
Frequently Asked Questions
nohup is a Linux command that allows a process to continue running even after the user who started it has logged out.
You can use the nohup command by typing nohup [command] & in the terminal, where [command] is the command you want to run.
The & symbol is used to run the command as a background process, allowing the terminal to return to the prompt and continue accepting other commands while the nohup process runs.
Yes, you can redirect the output of a nohup command by using the > symbol followed by a file name, such as nohup [command] > [file] &
Yes, you can stop a nohup command by using the kill command and specifying the process ID of the nohup process.
Leave a Reply