Updated April 20, 2023
Introduction of Linux Nohup
In the Linux ecosystem, when a client or user want to logout or sign out from the Linux environment then the sessions or the programs were opened by the user will close or terminated. If the user wants to keep a few programs or applications will open or it will be in running state after the user session will close. Then nohup utility comes in picture and with the help of nohup utility, we can keep the program or application in running state after the user will logout the Linux session.
The nohup command utility was written by Jim Meyering.
Syntax of Nohup Command
nohup COMMAND [ARG]... nohup OPTION
- nohup : We can use the nohup keyword in the syntax or command. It will work in two different ways i.e. command with arguments and with nohup options. As per the command input, the nohup will keep the job in running state and keep the standard output and error file.
- COMMAND : We can provide the compatible nohup command.
- OPTIO : We can provide the different flags as options that arecompatible with the nohup command.
- ARG : As per the requirement, we can pass the different arguments to nohup command.
How does Linux Nohup Command Work?
- In Linux, the nohup command is very suitable for those shell jobs that are taking too much time to execute. In this case, we need to use the nohup utility and execute those long-running shell jobs or applications jobs with the help of nohup utility. The nohup utility will keep the proper track on it and provide all the log related information with the assigned job with it.
- The nohup command will accept both the command with arguments and nohup option. As per the input, the nohup command will keep the jobs in the background and keep in running state.
- By default, the standard output of the nohup command will redirect to the nohup.out file. The nohup.out will present in the current working directory.
- The job will run with the help of nohup utility, the standard error will redirect to the nohup.out file i.e. in the nohup.out the file you will get both the standard output logs as well as the standard error logs.
- As per the requirement, we can redirect the output log file to different files also except nohup.out file.
Examples to Implement Linux Nohup Command
Following are the examples are given below:
It is a very simple and common way to use the nohup command in a Linux environment. It will execute any shell or program or command and when the Linux terminal will close still the program or shell job will run. The output logs will update in the nohup.out file.
Explanation: As per the above command, we are using nohup command with the date command. We are providing the command argument as input the nohup command. Once, we will execute the command. It will create the nohup.out file. The file will contain all the logs or command output related information in it (refer screenshot 1 (a)). The nohup.out fill will create automatically, once the job or command will trigger by the nohup command. Kindly find the screenshot 1 (b).
Screenshot 1 (a)
Screenshot 1 (b)
2. Nohup Command – Run Job in Background
In nohup command, we are having the functionality to run the jobs or commands in the background. To run the jobs in the background, we need to use the “&” with the nohup command.
nohup ./test_job.sh &
Explanation: With the help of nohup command, we are able to trigger the job in the background and the keep the proper job tracking with the help of process id (1969) and the nohup.out file. As per the below screenshot 2 (a), we have written one bash job “test_job.sh”. With the help of nohup command, we are executing the bash job and the job will run in the background.
Once the job will execute, it will display the integer number. The integer number is nothing but a process id. The process id will help to track the backend process. We can also kill the job with the help of the same process id (refer screenshot 2 (b)).
Screenshot 2 (a)
ps -ax | grep -i 1969
Screenshot 2 (b)
3. Nohup Command – Redirecting Output File
In nohup command, we are able to assign the own output or log file to any nohup command. It will create the own file at own location. To use the own log file, we need to use the redirection option in the nohup command.
nohup ./test_job.sh > test_job_log.out 2>&1 &
Explanation: By default in nohup command, it will create the nohup.out the file to store the job or command logs as well as the command logs. But if we do not want to store the logs nohup.out file. Then there is provision to keep or create the own log file for nohup command. As per the above command, we have redirected the log output to “test_job_log.out” file in the current working directory.
Note: As per the requirement, we can define or create a log file in a different location.
Screenshot 3 (a)
Screenshot 3 (b)
4. NohupCommand – Help
In nohup command, we are having the functionality to understand the nohup utility or command.
Explanation: As per the above command, we are able to find the basic understanding of nohup command. It will display the number of default command available in the nohup utility.
In nohup command, we are able to find the nohup version in the current Linux operating system.
Explanation: As per the above command, we will get the version information of the nohup utility.
We hope that this EDUCBA information on “Linux Nohup” was beneficial to you. You can view EDUCBA’s recommended articles for more information.