Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Leaving background jobs running in SSH 2

Status
Not open for further replies.

thedaver

IS-IT--Management
Jul 12, 2001
2,741
US
Anyone have a reference that explains the behavior of SSH and jobs running in the background?

I'm trying to figure out if I run a job, say
"webalizer -Q &" during an interactive SSH shell and then disconnect the SSH shell, whether there are settings that dictate whether that running script survives my disconnection or not.

Is this super obvious or are there tweaks to the shell or SSH that define whether the job will continue running if disconnected?

Thx, D.

D.E.R. Management - IT Project Management Consulting
 
nohup webalizer -Q &
will keep your job running when you log off.
 
A daemon is a process that closes stdin, stdout, and stderr when it starts up. Any process that closes those three file handles (ie diverts all output to a non-tty) will stay running when you close the shell that you started it in.

nohup is a very good way to do this.

Another flexible method is to use screen. The best way to explain is to give an example:
[ol]
[li]I ssh to my webserver as todd@www.webserver.com[/li]
[li]I run 'screen'. This starts a new bash shell inside of a screen session.[/li]
[li]I run the webalizer command.[/li]
[li]I press 'Ctrl-A Ctrl-D' to "detach" from the session. It and the process you started are both still running.[/li]
[li]I logoff the machine, leave work, get home.[/li]
[li]I ssh to my webserver as todd@ (must be the same user I ssh'd as in step 1).[/li]
[li]I run 'screen -x'. This "attaches" me to the session started earlier. (-x only works if there is just one screen session. If you have multiple screen sessions, then screen -x prints them all out and you need to use screen -r PID to attach to one).[/li]
[li]I can either type 'exit' to end the session or 'Ctrl-A Ctrl-D' to detach again.[/li]
[/ol]
Note that screen is a powerful tutoring utility as well. I could be connected to my webserver in a screen session, you could ssh to my machine as my user and attach to my screen session, and then you will be able to see what I do. To restate, more than one person can attach to a screen session so that all can see what is going on.

This is a lot of work though. nohup sounds like the easier route to take :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top