Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Keeping jobs going after closing ssh shell?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
qwkbrnfox
Apprentice
Apprentice


Joined: 05 Nov 2002
Posts: 231
Location: Vancouver, BC, Canada

PostPosted: Tue Jul 06, 2004 12:47 am    Post subject: Keeping jobs going after closing ssh shell? Reply with quote

OK, let's say I'm away from home, and I've decided that this is the perfect time to emerge a couple of giant packages on my home machine. I ssh to the machine, then
    emerge blah-blah >& dump &
which seems to be working fine. However, when I Ctl-d to leave the shell - hoping to leave the emerge running after I log out - the shell just hangs. It won't logout properly until I open another shell, log in via ssh again, and kill the supposedly background process.
How can I start a job via ssh that is fully independent of the shell, so I can leave?

Thanks - and sorry about the newbie question :wink:
Back to top
View user's profile Send private message
Lajasha
Veteran
Veteran


Joined: 17 Mar 2004
Posts: 1040
Location: Vibe Central

PostPosted: Tue Jul 06, 2004 12:52 am    Post subject: Reply with quote

Well you have 2 options the first I will give is simply how to make things run in the background the second is the way that I prefer an application called screen.

To run an application in the background simply append an "&" to the end of it ex.
Code:
emerge -Duv world &
This would run the update world in the background and should survive the shell being shutdown, however you will not have a way to bring it back to the foreground when you reconnect.

Enter Screen:
I honestly can not say enough about this app. I absolutly love it. It will allow you to run what ever you want then Detatch from it and close your shell and then open the shell again later and re-attach to the session and it is like you never left. The applications that you had running stay running and you can even have multiple screens going at once. If you want to give this one a go just "emerge screen" I believe will get it for ya.
_________________
Come and play in my land
Back to top
View user's profile Send private message
qwkbrnfox
Apprentice
Apprentice


Joined: 05 Nov 2002
Posts: 231
Location: Vancouver, BC, Canada

PostPosted: Tue Jul 06, 2004 1:02 am    Post subject: Reply with quote

maletek wrote:

To run an application in the background simply append an "&" to the end of it ex.
Code:
emerge -Duv world &
This would run the update world in the background and should survive the shell being shutdown, however you will not have a way to bring it back to the foreground when you reconnect.

Sorry, it didn't work. Weird. I'm using ssh here, so something funny seems to be going on. I can put it into the background locally, and close the shell no problem.

I'll take a look at screen, too. I'm looking for something quick here, but that looks like a pretty useful app.
Back to top
View user's profile Send private message
apeitheo
Apprentice
Apprentice


Joined: 09 Jan 2004
Posts: 222

PostPosted: Tue Jul 06, 2004 1:13 am    Post subject: Reply with quote

Would using nohup work? Maybe? (Not sure if it would work through an ssh session)
Code:
# nohup emerge world

As for screen (wonderful program btw), that should work.


Last edited by apeitheo on Tue Jul 06, 2004 1:15 am; edited 1 time in total
Back to top
View user's profile Send private message
qwkbrnfox
Apprentice
Apprentice


Joined: 05 Nov 2002
Posts: 231
Location: Vancouver, BC, Canada

PostPosted: Tue Jul 06, 2004 1:14 am    Post subject: Reply with quote

Got it - why do I always find these things just after I post? I have to use:
Code:
emerge blah-blah < /dev/null >& outfile &
Note the input redirection. This is a quirk of ssh. And *ahem* it's in the FAQ for ssh :oops:
Back to top
View user's profile Send private message
Angrybob
Guru
Guru


Joined: 19 Apr 2003
Posts: 575

PostPosted: Tue Jul 06, 2004 5:18 pm    Post subject: Reply with quote

you need to use a clever little program called screen. run screen then type in your command, you can now kill your ssh session and the command will carry on running in the background. if you want to see how it's doing then log back in with ssh and do "screen -r" to resume your last session.

here's a better description: http://www.kuro5hin.org/story/2004/3/9/16838/14935

[edit] oops, only skimmed the posts didn't notice that screen was already mentioned... but anyway, it is the answer
Back to top
View user's profile Send private message
Digital Storm
Tux's lil' helper
Tux's lil' helper


Joined: 07 Mar 2003
Posts: 116
Location: Toronto, Canada

PostPosted: Tue Jul 06, 2004 7:22 pm    Post subject: Reply with quote

You should also be able to use

Code:
nohup emerge world &
Back to top
View user's profile Send private message
carlosalvatore
n00b
n00b


Joined: 22 Nov 2011
Posts: 13

PostPosted: Tue May 05, 2015 8:25 am    Post subject: Reply with quote

qwkbrnfox wrote:
Got it - why do I always find these things just after I post? I have to use:
Code:
emerge blah-blah < /dev/null >& outfile &
Note the input redirection. This is a quirk of ssh. And *ahem* it's in the FAQ for ssh :oops:


You are my personal hero! 8)
_________________
Cowabunga!
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Tue May 05, 2015 11:55 am    Post subject: Reply with quote

What Digital Storm said.

Yeah screen/tmux, but if nohup does the job, it's less hassle.
Back to top
View user's profile Send private message
miket
Guru
Guru


Joined: 28 Apr 2007
Posts: 488
Location: Gainesville, FL, USA

PostPosted: Wed May 06, 2015 6:48 pm    Post subject: Reply with quote

qwkbrnfox wrote:
Got it - why do I always find these things just after I post? I have to use:
Code:
emerge blah-blah < /dev/null >& outfile &
Note the input redirection. This is a quirk of ssh. And *ahem* it's in the FAQ for ssh :oops:

Actually, I wouldn't call this a quirk. Simply backgrounding a process makes it so that the new process inherits fd's from its parent; those fd's become unusable when the ssh process exits.

One way to avoid this problem in spawning background processes (and a typical way to spawn processes to run as daemons) is to close the child's fd's before executing the daemon. Most software, though, does badly if started with fd's 0, 1, and 2 closed. The typical thing, then, is instead of just closing those fd's is to reopen them to /dev/null. You're command line is doing almost that; the difference is that it opens stdout and stderr to a file. That's good too.

I'm a big fan of the screen approach. If the merge goes off the rails, you can see what went wrong and can make needed corrections while you have your command history still easily available (command history around the emerge is likely to drift away in the noise of other commands issued under the same user in other sessions). You can detach the screen and go away and even reopen it later at a different computer. I use a build host separate from my main machine. I usually have the screen session that contains the build chroot alive for months at a time. I connect back to it when I need to build a new package or do an update.
Back to top
View user's profile Send private message
luismw
Tux's lil' helper
Tux's lil' helper


Joined: 04 Jan 2010
Posts: 91

PostPosted: Thu May 07, 2015 5:52 pm    Post subject: Reply with quote

Wouldn't "disown" work in this situation?
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3922
Location: Hamburg

PostPosted: Thu May 07, 2015 6:08 pm    Post subject: Re: Keeping jobs going after closing ssh shell? Reply with quote

qwkbrnfox wrote:
How can I start a job via ssh that is fully independent of the shell, so I can leave?
Code:
nohup ./my_very_long_lasting_script.sh &
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3135

PostPosted: Fri May 08, 2015 9:21 pm    Post subject: Reply with quote

Nohup is nice but it can only run programs. However there is also a way that works even with shell's runtime defined functions... and it is to be used on already running processes:
$ stuff_i_want_to_run & disown $!

which means: run some command in the background, and call disown with it's PID
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3922
Location: Hamburg

PostPosted: Sun May 10, 2015 3:44 pm    Post subject: Reply with quote

szatox wrote:
Nohup is nice but it can only run programs
erm,
Code:
nohup emerge blah-blah >& dump &
should work fine
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3135

PostPosted: Sun May 10, 2015 10:11 pm    Post subject: Reply with quote

Emerge is a named program accessible with some path. A function defined in shell during runtime is not.
: () { sleep 60; echo "something illustrative and harmless"; }
nohup :
won't work, but
: & disown $?
will do just fine

Rendered harmless to the unwary. -- desultory
Back to top
View user's profile Send private message
Naib
Watchman
Watchman


Joined: 21 May 2004
Posts: 6051
Location: Removed by Neddy

PostPosted: Sun May 10, 2015 10:18 pm    Post subject: Reply with quote

szatox wrote:
Emerge is a named program accessible with some path. A function defined in shell during runtime is not.
: () { :&:;}
nohup :
won't work, but
: & disown $?
will do just fine


bad form recommending a forkbomb!


I would recommend tmux, fantastic terminal multiplexer. It essentially does what screen does but I have had less issues with it than with screen. This is viable if you need to reconnect to see the output or carry on.
If you just want to launch and forget then nohup or at
_________________
Quote:
Removed by Chiitoo
Back to top
View user's profile Send private message
Malaki
n00b
n00b


Joined: 14 Nov 2011
Posts: 19
Location: Montreal, Québec, Canada.

PostPosted: Thu May 14, 2015 6:28 am    Post subject: Reply with quote

I also recommend tmux over screen.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum