LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 06-02-2008, 03:01 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 419

Rep: Reputation: 31
bash - ssh login and return


i have this in a bash script:
Code:
plz=0
box=1
ipadd=(i have this in an if/then condition providing various addresses)

ssh "processing-"$plz$box"@"$ipadd" /home/processing-"$plz$box"/Documents/Scripts/script_to_run.sh &"
it is within a loop that increments the 'box' variable
to copy files to a series of remote computers sequentially.

it gets as far as the ssh login correctly but does not return
from the remote after starting the script named 'script_to_run.sh.'
i get a warning that:
Quote:
'Permanently added '192.168.x.x /home/processing-01/documents/scripts/script_to_run.sh'
(RSA) to the list of known hosts.'
i notice that, in this message, the paths documents and scripts
are not captialized. in the system, though, they are capitalized.
the capitalization is correct in the script. the login occurs but
the script is not run.

if i go over to the remote box and run the 'script_to_run.sh'
script locally on the box, it runs fine.

what i want is to have the loop login via ssh, start the remote
script, then immediately return, leaving the remote script sunning.
this worked before i incorporated variables into the command. i
used the '&' at the end to return after starting the remote script.
now i'm inside quotation marks and unsure of the syntax to
accomplish what i'm trying to do.

how do i get this to work now?

thanks,
BabaG

Last edited by babag; 06-02-2008 at 03:35 PM. Reason: added $ipadd variable
 
Old 06-02-2008, 03:28 PM   #2
kdingo
LQ Newbie
 
Registered: Jun 2005
Location: WA
Distribution: openSUSE 10.3, CentOS 5
Posts: 7

Rep: Reputation: 0
You may want to try the -f option for ssh. It worked for me when i tried this, and should behave the same for your script.

Code:
ssh -f xx.xx.xx.xx "sleep 10"
 
Old 06-02-2008, 04:10 PM   #3
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks kdingo. that wasn't it for the returning part. got the syntax to
work like this:
Code:
ssh "processing-"$plz$box"@"$ipadd" /home/processing-"$plz$box"/Documents/Scripts/script_to_run.sh "&
subtle. only change is at the end of the line where i changed:

_run.sh &"

to

_run.sh "&


now the problem is that, while the loop works as far as login and return,
the 'script_to_run.sh' script never runs. could this be the capitalization
issue described above?

any ideas, anyone?

thanks again,
BabaG

Last edited by babag; 06-02-2008 at 04:35 PM.
 
Old 06-02-2008, 06:37 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You need

nohup myprog &

the 'nohup detaches it from the login terminal, otherwise, even though the '&' sends it to the background, its still logically attached to the term.
You can see this if you initiate a long sleep eg "sleep 10" inside a script and then background the script and try to logout immediately.
It'll warn you you have jobs running and ask if you really want to exit, as this will kill the script.
 
Old 06-02-2008, 08:13 PM   #5
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks chrism01.

how would i use the nohup command in context of this line?
Code:
ssh "processing-"$plz$box"@"$ipadd" /home/processing-"$plz$box"/Documents/Scripts/script_to_run.sh "&
the problem i'm having now is that the above line does not work,
but if i explicitly give each ssh command, it works:
Code:
if [ $box -eq 1 ]; then
   ssh processing-01@192.168.1.101 /home/processing-01/Documents/Scripts/script_to_run.sh &
else
   if [ $box -eq 2 ]; then
      ssh processing-02@192.168.1.102 /home/processing-02/Documents/Scripts/script_to_run.sh &
   else
      if [ $box -eq 3 ]; then
         ssh processing-03@192.168.1.103 /home/processing-03/Documents/Scripts/script_to_run.sh &
      else
         if [ $box -eq 4 ]; then
            ssh processing-04@192.168.1.104 /home/processing-04/Documents/Scripts/script_to_run.sh &
         else
            if [ $box -eq 5 ]; then
               ssh processing-05@192.168.1.105 /home/processing-05/Documents/Scripts/script_to_run.sh &
            else
               if [ $box -eq 6 ]; then
                  ssh processing-06@192.168.1.106 /home/processing-06/Documents/Scripts/script_to_run.sh &
               fi
            fi  
         fi
      fi
   fi
fi
what's wrong here and how would i use nohup to make it work?

fwiw, i do have nohup in use in the 'script_to_be_run,sh' script
and it is similarly set up as to what you've said.

thanks,
BabaG

Last edited by babag; 06-02-2008 at 09:07 PM. Reason: fix '-eq'
 
Old 06-02-2008, 08:23 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
1. I don't have a passwordless system to test on, so this is theoretical
2. you need to nohup the actual calling of the shell script to detach the login, something like:

ssh processing-01@192.168.1.101 nohup /home/processing-01/Documents/Scripts/script_to_run.sh &

possibly with quotes around the cmd to make sure its all executed remotely eg

ssh processing-01@192.168.1.101 "nohup /home/processing-01/Documents/Scripts/script_to_run.sh &"

3. In your shell scritp, use if/then/elif/elif/.../else/fi ... instead of if/then/else/if/then/else ....
http://www.tldp.org/LDP/abs/html/testconstructs.html

4. Numerical comparison:
use -eq not =

http://www.tldp.org/LDP/abs/html/comparison-ops.html

HTH
 
Old 06-02-2008, 09:10 PM   #7
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks again chrism01.
Quote:
4. Numerical comparison:
use -eq not =
will look into the other things when i get in tomorrow.
for now, i've edited the previous post to fix this. i've
actually had it right in the actual script.

thanks again,
BabaG
 
Old 06-03-2008, 02:48 PM   #8
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
ok. new day here. fixed the if/elif stuff. tried this:
Code:
plz=0
box=1
ipadd=(i have this in an if/then condition providing various addresses)

ssh "processing-"$plz$box"@"$ipadd" nohup /home/processing-"$plz$box"/Documents/Scripts/script_to_run.sh &"
pretty much the same behaviour as before. adding 'nohup' to the ssh
command didn't seem to change anything. when the remote is called
'script_to_run.sh' does not run.

i do get this, though:
Quote:
Psuedo terminal will not be allocated because stdin is not a terminal.
followed by the warning about permanently adding to the list of known hosts.

any ideas anyone? i think i'll pursue this in the networking forum.
starting to seem like might be more of an ssh thing than bash.

thanks,
BabaG

Last edited by babag; 06-03-2008 at 03:04 PM.
 
Old 06-03-2008, 04:53 PM   #9
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
found a solution and posted in this thread in the
networking forum:

http://www.linuxquestions.org/questi...script-646761/

thanks to all,
BabaG
 
  


Reply



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
bash - return home path babag Programming 2 06-01-2008 04:59 PM
Bash script: return code ftp TalkingMarble Linux - Newbie 3 05-09-2008 09:37 AM
Bash function return rejeep Programming 15 08-24-2007 04:21 AM
bash script Want to capture return key and assign a value procfs Programming 9 07-07-2006 01:38 AM
Bash return question merchtemeagle Programming 2 11-02-2005 10:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:48 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration