Disconnect from SSH with command running - Shell script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Disconnect from SSH with command running - Shell script
Hi,
I need to login on a remote machine, execute a command that will keep running after terminate the session.
Right now i able to connect to remote session but i can't disconnect (tried with logout or exit).
Somewhere i saw a post sugesting to kill the ssh process while connected to the remote machine but i want to avoid that.
Have a look at 'man nohup'. The 'nohup' allows you to run a command that does not hang up when the shell ends.
Code:
nohup foo &
The ampersand puts it into the background (ie, lets the parent shell continue), and the nohup prevents 'foo' from being terminated when the parent shell ends.
Note that doing this means that 'foo' cannot take input from the terminal, and any output will be directed to 'nohup.out'.
You do not always need the nohup (some programs disassociate themselves from the parent, eg most daemons).
Last edited by neonsignal; 08-12-2009 at 07:09 PM.
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
I'll second GNU Screen. Google "howto gnu screen", and you will get a bunch of good introductions and tutorials on the first page of hits. I used it to set up a terminal server on an old iMac -- http://blogs.umass.edu/choogend/2008/05/23/ammonoidea/. It automatically starts up with the screen sessions connected to the consoles. I can ssh to it and connect to the consoles and see their whole history as well as interact. Really cool tool.
As i explained the procedure is start the server iperf process on the server, then disconnect and start the client on the local machine. Is it is right now remains in Step 2.
Can you help me?
I'm not sure why your sequence is not working; I just tried something similar here, and the ssh hung up with no problems.
eg
Code:
ssh -T server <<SESSION
iperf -s -P 0 -i 5 -p 5001 -f k > client &
SESSION
printf "done"
You could try redirecting all of the iperf streams (in case you are getting errors), eg
iperf -s -P 0 -i 5 -p 5001 -f k >client 2>client.err </dev/null &
It seems you don't need the nohup because iperf detaches itself already.
Why do you have an 'exit' after step 3? Won't that just abort your script? (I'm assuming this is in a script).
You need to check if the process is already running in the remote session, because in case he checks that iperf is running and just close properly. Only if isn't running hangs on second step.
I need to exit from remote ssh session and execute more commands locally.
Yes, fair enough that you check for the process already running; I was just demonstrating that the important part works.
Quote:
I need to exit from remote ssh session and execute more commands locally.
The remote ssh session finishes as soon as you reach the tag 'SESSION', because this will send an EOF (end of file) to the ssh. So after the line 'printf "Step3"', the script itself will exit, and the local (client) iperf is never run.
The only thing that is surprising in your case is that you seem to be saying it doesn't even print "Step3".
Yes, fair enough that you check for the process already running; I was just demonstrating that the important part works.
The remote ssh session finishes as soon as you reach the tag 'SESSION', because this will send an EOF (end of file) to the ssh. So after the line 'printf "Step3"', the script itself will exit, and the local (client) iperf is never run.
The only thing that is surprising in your case is that you seem to be saying it doesn't even print "Step3".
It seems that the ssh session don't finish with the SESSION.
I did some tests and for sure the problem is related with the iperf server start. I tried the same script but changing nohup iperf -s -P 0 -i 5 -p 5001 -f k > client & to ls and it worked.
Quote:
Is it possible that there is some white-space after SESSION?
No. I checked and there are no spaces.
Quote:
I would also suggest replacing ps ax |grep -v grep | grep with pgrep -f.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.