LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 08-12-2009, 06:43 PM   #1
striker
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Rep: Reputation: 0
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
 
Old 08-12-2009, 06:57 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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.
 
Old 08-12-2009, 07:12 PM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
Also: GNU screen. It's popular enough that it is often part of a "base installation" for many distros.
 
Old 08-12-2009, 07:43 PM   #4
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,189

Rep: Reputation: 105Reputation: 105
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.
 
Old 08-15-2009, 03:54 PM   #5
striker
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 08-15-2009, 08:25 PM   #6
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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).
 
Old 08-16-2009, 05:44 AM   #7
striker
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 08-16-2009, 08:22 AM   #8
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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".
 
Old 08-16-2009, 12:49 PM   #9
striker
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neonsignal View Post
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
 
Old 08-16-2009, 02:11 PM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
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.
 
Old 08-16-2009, 04:49 PM   #11
striker
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ntubski View Post
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.
 
Old 08-16-2009, 05:03 PM   #12
striker
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 08-24-2009, 05:58 PM   #13
striker
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by striker View Post
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
 
Old 08-24-2009, 08:18 PM   #14
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error running command via shell script lowlifeish Linux - Software 2 08-31-2007 10:38 PM
running a command with args from a shell script KM3 Programming 3 10-19-2006 01:46 PM
running shell command on remote machine with ssh qrshat Solaris / OpenSolaris 3 08-17-2006 07:32 AM
Keep running a command when disconnect from SSH stuartornum Linux - General 3 02-17-2006 10:19 PM
Running a shell script remotely via 'ssh <hostname> <command>' davee Linux - General 4 10-09-2005 01:38 AM


All times are GMT -5. The time now is 04:58 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration