Вы находитесь на странице: 1из 3

Unix Nohup: Run a Command or Shell-Script Even after You Logout

http://linux.101hacks.com/unix/nohup-command/

Linux 101 Hacks

Unix Nohup: Run a Command or Shell-Script Even after You Logout


by SathiyaMoorthy 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 youve 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 dont 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 youve 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 ..

1 of 3

4/9/2014 7:09 AM

Unix Nohup: Run a Command or Shell-Script Even after You Logout

http://linux.101hacks.com/unix/nohup-command/

If you log-out of the shell and login again, youll 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

{ 17 comments read them below or add one } 1 Dana Marble July 26, 2010 at 12:12 pm 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. 2 benjamin December 1, 2010 at 7:13 am 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 3 Badal January 5, 2012 at 11:13 am 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 4 Shokat Sidat January 6, 2012 at 1:43 am 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. 5 abc April 14, 2012 at 6:57 am 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? 6 ashutoshh singh August 8, 2012 at 10:43 pm this is really pretty useful in shell scripting and ssh server tooo 7 pradip garala February 15, 2013 at 8:57 pm good article. 8 Eyal March 4, 2013 at 10:39 am sorry where do I download nonhup? Thanks 9 Bilal March 20, 2013 at 1:35 pm 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 10 Shridhar April 17, 2013 at 8:08 am 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 11 cesarjob April 17, 2013 at 8:30 am But, if you send a jobs with & it will run after logout. Which is the difference? 12 configman April 18, 2013 at 2:20 am

2 of 3

4/9/2014 7:09 AM

Unix Nohup: Run a Command or Shell-Script Even after You Logout

http://linux.101hacks.com/unix/nohup-command/

@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. 13 Avijit mondal May 25, 2013 at 4:41 am pretty much cool nohup command, running any script. 14 mayhs June 18, 2013 at 3:17 am I use nohup command to run the shell script for running in the background which runs for atleast 7 hours. 15 prachi August 15, 2013 at 12:40 am 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. 16 vaibhaw kr singh September 30, 2013 at 12:02 am 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. 17 Azmath Mohamad March 13, 2014 at 11:37 am nohup helped me to home from work while job is still running till morning Leave a Comment Name * E-mail * Website

{ 4 trackbacks } Understand at, atq, atrm, batch Commands using 9 Examples 5 Ways to Execute UNIX / Linux Commands (and Shell Scripts) in Background How to Run Cron Every 5 Minutes, Seconds, Hours, Days, Months How to Setup Reverse SSH Tunnel on Linux Previous post: Tee Command Usage Examples Next post: How to View Windows CHM Files on Linux Home | Linux 101 Hacks - Table of Contents | Contact | Email | RSS | Copyright 20092013 Ramesh Natarajan. All rights reserved | Terms of Service

3 of 3

4/9/2014 7:09 AM

Вам также может понравиться