Nohup Command in Linux
In the Linux environment, controlling long-running processes and maintaining their continuity is critical, particularly for system administrators and developers. One of the most useful tools for this purpose is the nohup command. Short for "no hang up," nohup enables users to perform commands or scripts in the background, guaranteeing that they continue to execute even after logging out or losing connection. Understanding how to use nohup can dramatically improve a workflow, whether doing normal maintenance, running time-consuming scripts, or managing server activities.
Prerequisites Copy link
Make sure the following requirements are met before executing the nohup command in Linux to effectively manage long processes:
-
Access to a Linux terminal session where commands, such as
nohup, can be executed. -
To run commands, control processes, and explore directories, users must understand the fundamentals of Linux command line operations.
-
To perform commands and control processes on the system, make sure users have the necessary rights (particularly
rootorsudoaccess). -
To monitor and control processes that were started with
nohupcommand, it's helpful to have a basic understanding of the process management commands likepsandkill. -
Make sure the machine has enough storage space available, particularly if the command produces output that is saved to a file.
How nohup Works Copy link
The nohup command in Linux allows process to continue running even after the user logs out or when the terminal session ends. Normally, when a user signs out or disconnected of a terminal session, all related processes receive a hangup signal (SIGHUP). This signal usually causes processes to end. However, when a process starts with nohup, the SIGHUP signal is ignored. By default, nohup redirects the command's standard output (stdout) and standard error (stderr) streams to a file called nohup.out in the current directory. This allows users to record any output or error messages created by the command even after the terminal session has terminated. When appending an ampersand (&) to the nohup command, it runs the specified command in the background.
Examples and Best Practices for Using nohup Copy link
The following are some examples of using the nohup command in Linux.
-
Navigate to the directory where the script is located (
cd /home/testuser) and run the below command. This will execute the script in the background.
Syntax: nohup ./script name &
Wherein:
./indicates that the script will run in the current directoryscript nameis the name of the script to be run
nohup ./testscript.sh &Press Enter. The output of the script will be directed to file called nohup.out.
Validate it by running the command below.
ls -lrtTo check the logs generated by the script. View the content of the file named nohup.out by executing the command below.
cat nohup.out-
To do the output redirection, execute the below command to redirect the output of the script to a file name.
Syntax: nohup ./script name > filename
nohup ./testscript.sh > testscript.out.logView the content of the file named testscript.out.log by executing the command below.
cat testscript.out.log-
Aside from running a script, user can also run command in background using
nohup. To do this, run the command below and press Enter.
Syntax: nohup command &
nohup ps -ef &View the content of the file nohup.out to see the output that has been generated by the command (ps -ef). Execute the command below.
cat nohup.out-
Redirect the output of command using
nohup. Run the command below.
Syntax: nohup command > outputfile
nohup ps -ef > ps.out.logThen validate the output generated by running the command below.
cat ps.out.log-
Chaining multiple commands with
&&allowsnohupto execute them sequentially. See example below.
Syntax: nohup command1 && command2 && command3 &
nohup sleep 100 && echo “hello world” &Terminating a Process Started by Nohup Copy link
Let’s say the command below was executed.
nohup sleep 100 && echo “hello world” &-
First, use
psto determine the process ID (PID). Execute the command below.
ps aux | grep sleepWe can see that PID is 104539.
-
Kill the pid by running the command below.
kill 104539Conclusion Copy link
In conclusion, the nohup command is an extremely useful tool for anyone working with Linux (you can try our Linux VPS hosting for your projects), especially system administrators and developers who need to manage long-running processes. By allowing commands to persist even after the user logs out, nohup ensures that key processes are completed uninterrupted, which is critical for system stability and productivity.