
nohup
means no hang-up. This command allows Linux processes to continue running in the background even after logging out or closing the terminal. This is very useful for running long-duration scripts. Today we will learn how to use the nohup command to run shell script in background without interruptions.
Table of Contents
Run Shell Script in Background with NOHUP :
Suppose you have a shell script named test.sh
that executes a long-running task:
#!/bin/sh
echo "Task starts"
sleep 120
echo "Task ends"
To run this script in background , use below :
nohup sh test.sh &
Output :
nohup: ignoring input and appending output to 'nohup.out'
[1] 12345
nohup.out
stores the command’s output unless we give any log file name .[1] 12345
represents the background job ID (1
) and the process ID (12345
).
Store the Output to a Custom Log File :
By default, nohup
writes output to nohup.out
. You can redirect output to a specific log file:
nohup sh test.sh > test.log 2>&1 &
> test.log
redirects standard output totest.log
.2>&1
redirects standard error to the same log file.
Check Running Processes with nohup :
To verify if the script is running, use below :
ps -ef| grep test.sh
or,
ps -ef| grep nohup
Or, check background jobs in the current session :
jobs
Stop/kill a NOHUP Process :
Terminate it using the PID :
kill -9 <PID>
So, now you can understand how to use the nohup command to run shell script in background. Also you can get more information from the below Doc as well.
Related Oracle Related Articles :