LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Disconnect from SSH with command running - Shell script (https://www.linuxquestions.org/questions/programming-9/disconnect-from-ssh-with-command-running-shell-script-747137/)

striker 08-12-2009 06:43 PM

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

neonsignal 08-12-2009 06:57 PM

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).

anomie 08-12-2009 07:12 PM

Also: GNU screen. It's popular enough that it is often part of a "base installation" for many distros.

choogendyk 08-12-2009 07:43 PM

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.

striker 08-15-2009 03:54 PM

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

neonsignal 08-15-2009 08:25 PM

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).

striker 08-16-2009 05:44 AM

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.

neonsignal 08-16-2009 08:22 AM

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".

striker 08-16-2009 12:49 PM

Quote:

Originally Posted by neonsignal (Post 3645269)
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

ntubski 08-16-2009 02:11 PM

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.

striker 08-16-2009 04:49 PM

Quote:

Originally Posted by ntubski (Post 3645587)
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.

striker 08-16-2009 05:03 PM

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

striker 08-24-2009 05:58 PM

Quote:

Originally Posted by striker (Post 3645766)
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

neonsignal 08-24-2009 08:18 PM

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 02:22 PM.