Thursday, 24 October 2019

linux - How can I use nohup if the shell script taking more time to run?


My shellscripts takes really long time to run, around 30 minutes, but if I leave and go out for lunch I doubt that:


1: will putty session closes if the script is running?


2: if putty session closes then the script will exit out.


If I use nohup scriptname will it be fine?



Answer



PuTTY (or any other SSH client) does not care if you are looking at the screen or out to lunch. It will not close a session on its own.


A session will be closed only if:



  1. You close it manually (e.g., type logout, Ctrl+D, exit, etc etc)

  2. There is an auto logout or idle trigger set in your shell.

  3. The network connection dies.


The latter should not happen randomly, though poor equipment might 'helpfully' close connections when they think they are idle. This usually happens with SOHO equipment which come with very little memory and agressively close connection when they guess they are no longer used.




So far for puTTY and sessions. As for nohup, let's dive a little deeper.


In ye early days people used phone lines and modems to dial in. Sometimes that connection did die. In that case the shell gets a signal that the line has detected a hangup. (signal 1, HUP).


When that happens the shell would signal the running programs with SIGHUP, which basically meant 'the phone line dropped. Please shutdown neatly. (close files, save if needed etc etc).


When you log out or when puTTY (ssh) dies the same happens.


If you run a program with nohup then two things happen:



  1. SIGHUP gets ignored. So the program does not shut down.

  2. Standard output gets written to a file (default: nohup.out) rather than to the TTY.



If I use nohup scriptname will it be fine?



If you use nohup scriptname and the script does not need any input, then yes, that will be fine. If you want to see the output from the script while it is running you can tail the logfile.


nohup scriptname &
tail -f nohup.out


Besides nohup there are at least two other interesting options:



Both allow you not only to survive a disconnected line, but also to reattach a terminal at a later time.


Example:


Sitting behind a computer-1, logged into server-1



screen                         Starts screen
make bzimage                   Whatever task/script you want
Ctrl+A, Ctrl+D             Detach; screen seems to have ended
logout                         Logout



At this point screen is still running and providing a shell for your script.


Let's say you go home, sit down behind your own desktop and ssh to server-1,



ssh server-1.work.tld
  username
  password
screen -r                      (Continue a previously added screen)
   ︙                           Continue where you left off at work.



No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...