Ssh remote command doesn't work from script file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ssh remote command doesn't work from script file
# 1  
Old 05-09-2014
Ssh remote command doesn't work from script file

I have 10 application servers in a distributed architecture generating their own application logs. Each server has application utility to continuously tail the log. for example following command follows tails and follows new logfiles as they are generated
Code:
server1$ logutility logtype

When I run the same command from using ssh from a terminal window on a remote server using a loop for all servers that also works fine

Code:
remoteserver $ for host in server1 server2 ..... server10
do
ssh $host "logutility logtype" >> logfile &
done

Above works fine as long as my terminal is open but stops when I close my terminal. So I put the above in an executable scriptfile to run with nohop option

Code:
#!/usr/bin/ksh
for host in server1 server2 ..... server10
do
ssh $host "logutility logtype" >> logfile &
done

and then I run the scriptfile as follows

Code:
remoteserver$ nohup scriptfile &

However, those ssh commands terminate immediately.

This is strange problem that I am unable to understand. Why does ssh command continue to run when run from terminal but terminates immediately when run from script. I would like help in understanding this behavior and also what needs to be done so that I can run that command remotely and save collective output of all 10 servers to a file on remoteserver.

Last edited by jim mcnamara; 05-09-2014 at 01:10 PM..
# 2  
Old 05-09-2014
When run in the background ssh requires the -n option.

Code:
ssh -n servername 'command goes here ' &

These 3 Users Gave Thanks to jim mcnamara For This Post:
# 3  
Old 05-09-2014
Thankyou. I wasn't familiar with -n option, I did modify my scriptfile to use -n option but even after that the script doen't continue to run, I am
Code:
$ nohup scriptfile &
[1]     7160
$ Sending output to nohup.out

and as soon I press enter key I get the following
Code:
[1] +  Done                    nohup scriptfile &

I also checked ps -eaf | grep and didn't see any ssh command running to those servers

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks
# 4  
Old 05-09-2014
It is hard to tell what your script is doing - it will get a SIGTTOU or the like if it tries to write to stdout or stderr - which will cause what you see.

Let's start with ultra-basics:

Execute each code snippet from the command line:
someservername = a remote server

Code:
ssh -n someservername 'echo awake && /usr/bin/sleep 5' &

Next set of snippets:
Code:
ssh -n someservername 'echo awake && /usr/bin/sleep 5 ' > t.lis 

ssh -n someservername ' echo awake && /usr/bin/sleep 5' > t.lis &
wait

I hope you see what I'm referring to.

I would write your code like this to run everything at the same time (up to say 10 at once):

Code:
#!/bin/ksh
cnt=0
for sname in server1 server2 server3 server4 .... server99  
do
  cnt=$(( $cnt + 1 ))
  ssh $server 'my commands go here' >> logfile &
  [  $(( $cnt % 10  )) -eq 0 ]  && wait  # only run a max of 10 processes at once, you can change this
done
wait        # in case where number of server is not ( 0 == servercount % 10 )

Plus, you have to redirect stderr in the 'command goes here ' part to some useful place/file. Plus you should wait for child processes, not simply logoff.

Last edited by jim mcnamara; 05-09-2014 at 02:07 PM..
# 5  
Old 05-09-2014
I tested and those snippets work just fine. I ran ssh with -v -v -v option to capture debugging information.

Here is debug for ssh that kept running in the background.
Code:
debug1: ssh_session2_setup: id 0
debug1: Sending command: my command here
debug1: channel request 0: exec
debug2: callback done
debug1: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 131072
debug1: channel 0: read<=0 rfd 5 len 0
debug1: channel 0: read failed
debug1: channel 0: close_read
debug1: channel 0: input open -> drain
debug1: channel 0: ibuf empty
debug1: channel 0: send eof
debug1: channel 0: input drain -> closed

Here is debug where ssh session terminates after connecting.
Code:
debug1: ssh_session2_setup: id 0
debug1: Sending command: my command here
debug1: channel request 0: exec
debug2: callback done
debug1: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug1: channel 0: read<=0 rfd 5 len 0
debug1: channel 0: read failed
debug1: channel 0: close_read
debug1: channel 0: input open -> drain
debug1: channel 0: ibuf empty
debug1: channel 0: send eof
debug1: channel 0: input drain -> closed
debug1: channel 0: rcvd eof
debug1: channel 0: output open -> drain
debug1: channel 0: obuf empty
debug1: channel 0: close_write
debug1: channel 0: output drain -> closed
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: rcvd close
debug3: channel 0: will not send data after close
debug1: channel 0: almost dead
debug1: channel 0: gc: notify user
debug1: channel 0: gc: user detached
debug1: channel 0: send close
debug1: channel 0: is dead
debug1: channel 0: garbage collecting
debug1: channel_free: channel 0: client-session, nchannels 1
debug3: channel_free: status: The following connections are open:
  #0 client-session (t4 r0 i3/0 o3/0 fd -1/-1)

debug3: channel_close_fds: channel 0: r -1 w -1 e 7
debug1: fd 1 clearing O_NONBLOCK
debug1: Transferred: stdin 0, stdout 0, stderr 0 bytes in 0.2 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 0.0
debug1: Exit status 0

# 6  
Old 05-09-2014
Please post the exact code that you are trying to background.

Thanks.
# 7  
Old 05-09-2014
try doing it like this:

Code:
for x in server1 server2 .. server10
do
ssh $x "<command>" </dev/null > file.log
done

i would test with a command that wont hard the system.

Code:
/home/insta cat hostname.master|while read line
> do
> ssh "$line" "echo hi" < /dev/null > $line.log
> done

I just ran this on a few systems and it worked.
This User Gave Thanks to techy1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

PING to AIX works but TELNET FTP SSH doesn't work

root@PRD /> rsh DR KFAFH_DR: protocol failure due to unexpected closure from server end root@PRD /> telnet DR Trying... Connected to DR. Escape character is '^]'. Connection closed. root@PRD /> ftp DR Connected to KFAFH_DR. 421 Service not available, remote server has closed connection... (2 Replies)
Discussion started by: filosophizer
2 Replies

2. Shell Programming and Scripting

Execute ssh command with additional terminal command to any remote user not working script

Hello i am having an issue with bash script and this is the code now=$(cat hosts1.txt | awk '{print $2;}') while read n ;do ssh root@$now 'useradd test1; echo -e "test1\ntest1" | passwd test1 && echo "test1 ALL=(ALL:ALL) ALL" >> /etc/sudoers' When i execute only part with cat, it... (8 Replies)
Discussion started by: tomislav91
8 Replies

3. Shell Programming and Scripting

Ssh bash script exits without remote command completion

Hi, My goal is to connect from unix server A to windows server B and call a bat file on windows. I am able to succeed in remoting to windows and executing a command, the issue i am facing is the shell scrip is exiting without making sure of bat file success. Can you please help me in... (4 Replies)
Discussion started by: pxp018
4 Replies

4. AIX

2nd SSH doesn't work with AD

Recently I decided to intall second daemon of SSH for Winbind users. I mean I have configuration AIX + Samba + AD and I can login to the server via SSH with AD accounts to 22 port without any problems. But now I have second installation of OpenSSH and don't understand why I can't do the same... (6 Replies)
Discussion started by: jess_t03
6 Replies

5. Shell Programming and Scripting

bash script to execute a command remote servers using ssh

Hello, I am running into few issues, please suggest me what I am missing. I am running this script on a linux host. Main idea of this script is to, login to each host via ssh and get uid of user, service user that I trying to run this script, has already deployed ssh keys and provide sudo... (8 Replies)
Discussion started by: bobby320
8 Replies

6. UNIX for Dummies Questions & Answers

find command in shell script doesn't work

Hello all, Something strange going on with a shell script I'm writing. It's trying to write a list of files that it finds in a given directory to another file. But I also have a skip list so matching files that are in that skip list should be, well uhm, skipped :) Here's the code of my... (2 Replies)
Discussion started by: StijnV
2 Replies

7. Shell Programming and Scripting

executing command in a remote machine through ssh - shell script

Hi All, i have two machines like x and y . my requirement is i should connect to machine Y from x through ssh connection . and do some operation such as copy and move and delete files in Y machine . i tried with this code but it is doing in machine x only . and i need to exit from Y when... (1 Reply)
Discussion started by: rateeshkumar
1 Replies

8. UNIX for Dummies Questions & Answers

SSH-KEYGEN doesn't work

Hi, Am running the following commands on our server to generate a key for passwordless SSH, however we are getting 100% blank key files. E.g. when looking id_dsa.pub or id_dsa they are 100% empty - the files get created, but contain no content and have a file size of 0b. mkdir ~/.ssh... (3 Replies)
Discussion started by: gjp
3 Replies

9. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

10. Shell Programming and Scripting

command line work script doesn't

Hello, I'm stuck and confused as to why when I execute things form the command line it works but when in a script it doesn't. My script: ### creating a lock on the console touch /var/run/console.lock chmod 600 /var/run/console.lock echo "$User" >>... (2 Replies)
Discussion started by: larry
2 Replies
Login or Register to Ask a Question