Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
08-12-2009, 06:43 PM
|
#1
|
LQ Newbie
Registered: Aug 2009
Posts: 11
Rep:
|
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.
Thanks in advance
|
|
|
08-12-2009, 06:57 PM
|
#2
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
Have a look at 'man nohup'. The 'nohup' allows you to run a command that does not hang up when the shell ends.
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.
|
|
|
08-12-2009, 07:12 PM
|
#3
|
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Rep: 
|
Also: GNU screen. It's popular enough that it is often part of a "base installation" for many distros.
|
|
|
08-12-2009, 07:43 PM
|
#4
|
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
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.
|
|
|
08-15-2009, 03:54 PM
|
#5
|
LQ Newbie
Registered: Aug 2009
Posts: 11
Original Poster
Rep:
|
Hi,
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?
ssh -T root@192.168.1.133 <<SESSION
printf "Step1"
if ps ax |grep -v grep | grep "iperf -s -P 0 -i 5 -p 5001 -f k" > /dev/null
then
printf "Process was already running"
else
nohup iperf -s -P 0 -i 5 -p 5001 -f k > client &
printf "\nRunning.\n"
fi
printf "Step2"
SESSION
printf "Step3"
exit
printf "Step4"
iperf -c 192.168.1.133 -P 1 -i 5 -p 5001 -f k -t 10 -T 1 > server &
exit
Thanks
|
|
|
08-15-2009, 08:25 PM
|
#6
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
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).
|
|
|
08-16-2009, 05:44 AM
|
#7
|
LQ Newbie
Registered: Aug 2009
Posts: 11
Original Poster
Rep:
|
Hi,
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.
|
|
|
08-16-2009, 08:22 AM
|
#8
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
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".
|
|
|
08-16-2009, 12:49 PM
|
#9
|
LQ Newbie
Registered: Aug 2009
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by neonsignal
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.
Any sugestions?
Thanks
|
|
|
08-16-2009, 02:11 PM
|
#10
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,823
|
Could you paste the exact output you get from that script, it's not really clear what is happening at this point.
Is it possible that there is some white-space after SESSION?
I would also suggest replacing ps ax |grep -v grep | grep with pgrep -f.
|
|
|
08-16-2009, 04:49 PM
|
#11
|
LQ Newbie
Registered: Aug 2009
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by ntubski
Could you paste the exact output you get from that script, it's not really clear what is happening at this point.
|
[root@Server benchtools]# ./test
Step1
Running.
Step2
It's hangs here.
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.
|
Thanks for the suggestion.
|
|
|
08-16-2009, 05:03 PM
|
#12
|
LQ Newbie
Registered: Aug 2009
Posts: 11
Original Poster
Rep:
|
Hi, good news my friends. With your help know it's working. Thank you all. This was missing </dev/null Can you explain what it does?
#!/bin/sh
ssh -T root@192.168.1.133 <<EOF
if pgrep -f "iperf -s -P 0 -i 5 -p 5001 -f k" > /dev/null
then
printf "Process was already running"
else
iperf -s -P 0 -i 5 -p 5001 -f k >client 2>client </dev/null &
printf "\nRunning.\n"
fi
EOF
iperf -c 192.168.1.133 -P 1 -i 5 -p 5001 -f k -t 10 -T 1 > server &
exit
|
|
|
08-24-2009, 05:58 PM
|
#13
|
LQ Newbie
Registered: Aug 2009
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by striker
iperf -s -P 0 -i 5 -p 5001 -f k >client 2>client </dev/null &
|
Hi again,
How can i redirect both outputs to the same file without specify both?
Thanks
|
|
|
08-24-2009, 08:18 PM
|
#14
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
Code:
iperf -s -P 0 -i 5 -p 5001 -f k &>client </dev/null &
This is for the bash shell; some other shells may have slightly different syntax.
|
|
|
All times are GMT -5. The time now is 03:19 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|