When you execute a Unix job in the background ( using &, bg command), and logout from the session, your process will get killed. You can avoid this using several methods — executing the job with nohup, or making it as batch job using at, batch or cron command.
This quick tip is for beginners. If you’ve been using nohup for a while, leave us a comment and tell us under what situations you use nohup.
In this quick tip, let us review how to make your process running even after you logout, using nohup.
Nohup stands for no hang up, which can be executed as shown below.
nohup syntax:
# nohup command-with-options &
Nohup is very helpful when you have to execute a shell-script or command that take a long time to finish. In that case, you don’t want to be connected to the shell and waiting for the command to complete. Instead, execute it with nohup, exit the shell and continue with your other work.
Explanation about nohup.out file
By default, the standard output will be redirected to nohup.out file in the current directory. And the standard error will be redirected to stdout, thus it will also go to nohup.out. So, your nohup.out will contain both standard output and error messages from the script that you’ve executed using nohup command.
Instead of using nohup.out, you can also redirect the output to a file using the normal shell redirections.
Example: Printing lines to both standard output & standard error
while(true) do echo "standard output" echo "standard error" 1>&2 sleep 1; done
Execute the script without redirection
$ nohup sh custom-script.sh & [1] 12034 $ nohup: ignoring input and appending output to `nohup.out' $ tail -f nohup.out standard output standard error standard output standard error ..
Execute the script with redirection
$ nohup sh custom-script.sh > custom-out.log & [1] 11069 $ nohup: ignoring input and redirecting stderr to stdout $ tail -f custom-out.log standard output standard error standard output standard error ..
If you log-out of the shell and login again, you’ll still see the custom-script.sh running in the background.
$ ps aux | grep sathiya sathiya 12034 0.0 0.1 4912 1080 pts/2 S 14:10 0:00 sh custom-script.sh
Comments on this entry are closed.
I use nohup in combination with the watch command to move filtered syslog files to a particular directory, but only when I need them for a little while. Rather than use a cron job I set a watch command up for x amount of seconds and run a script to pull the correct logs, filter them and submit them to an ftp or email output. using nohup I can allow this process to run even while I am not logged into my station and close it at a later time (i.e a few hours or minutes) – this is the way I use the nohup command most often.
And i used nohup to have my google android ‘repo sync’ (over 5gb) to run in background, even if i close the computer.
On our slow network this sync would take alot of time, so making sure that process doesnt die if i accdently close putty window or have power surge, is very comforting. :S
I use the nohup command to restart tomcat server. Tomcat server was controlling both: 1) Application and 2) Jenkins. Jenkins is a CI tool. I have a script that stops and starts tomcat server from Jenkins. But after running stop script, tomcat server was shut down as a result Jenkins was not up. So Jenkins was unable to kick start script of tomcat. Using nohup, Jenkins kicked off stop and start script. Even though Jenkins was down, tomcat start script got executed. Of course, there are more elegant ways of achieving the same by stopping and starting only the context of the application. Looking into that now… 🙂
I use nohup when I am capturing performance related data during soak and stress testing. Run this through a script that will perform a disk and memory check every so often and capture the output for analysis later. This could run over a week.
how we see that which user is logged in and logged out within five minutes by using who command in shell script program in linux?
this is really pretty useful in shell scripting and
ssh server tooo…
good article….
sorry where do I download nonhup?
Thanks
Well I have designed a call centre wallboard. Uptil now I was using crontab.hourly to refresh my board and I desperately needed a solution. Finally I think nohup is the solution. I shall be implementing it tomorrow lets see what happens
I use nohup command for syncing and building android builds.Once job started from REMOTE connection (Putty) I can close same connection and wait for hours. This helps me to save *some* internet bandwidth of over remote connection 😉
But, if you send a jobs with & it will run after logout.
Which is the difference?
@cesarjob;
& sends the process to the background but it is still tied to your shell process – if the shell dies it will die. nohup does not background the task, but disconnects its standard in and out from the shell so that it is no longer dependent on it. To safely close your shell you need to both put the process in the background and disconnect it.
pretty much cool nohup command, running any script.
I use nohup command to run the shell script for running in the background which runs for atleast 7 hours.
hi!
I am running a script in side another script as below:
#!/usr/bin/ksh
###########################################################################################################################
#Purpose:Script for:Running test on diffrent AIX lpars
###########################################################################################################################
clear
i=0
cat list.txt | while read LINE
do
i=$((i+1))
echo “Running CloseCase on host:$LINE”
sshcmd -s $LINE “cd /appl/prachi/script/;nohup sh ./runCloseCaseManually.sh closeCases$i.csv &”
done
the .csv files are taken as inputs here with i as the serial order in their naming conventions.. i want to run 1 CSV file at one lpar so i have mentioned the lpars under list.txt. The nohup i have used here is still not helping me and is being killed as it moves on to next lpar in the loop.
if we are 3 batches parallel (‘A.ksh’,B.ksh,C.ksh run parallel and A.ksh will completed then D.ksh will run ) we have to handle the error also for all the batches..if u have the solution kindly send me on my mail.
nohup helped me to home from work while job is still running till morning 🙂