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 |
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 & 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). |
Also: GNU screen. It's popular enough that it is often part of a "base installation" for many distros.
|
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.
|
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 |
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 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). |
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. |
Yes, fair enough that you check for the process already running; I was just demonstrating that the important part works.
Quote:
The only thing that is surprising in your case is that you seem to be saying it doesn't even print "Step3". |
Quote:
Any sugestions? Thanks |
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. |
Quote:
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:
Quote:
|
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 |
Quote:
How can i redirect both outputs to the same file without specify both? Thanks |
Code:
iperf -s -P 0 -i 5 -p 5001 -f k &>client </dev/null & |
All times are GMT -5. The time now is 02:22 PM. |