Operating System - Linux
1752587 Members
3969 Online
108788 Solutions
New Discussion юеВ

while loop exits after call to ssh

 
Kevin Nikiforuk
Valued Contributor

while loop exits after call to ssh

The DBA team I work with has a shell script for starting and stopping their databases that uses sudo (1.6.6) to change to another account, then ssh (A.03.81.002) to a remote server to work with the Oracle Application Stack. Here is a code snippet:

==============================================
ORATAB=/etc/oratab

trap 'exit' 1 2 3
case ${ORACLE_TRACE} in
T) set -x ;;
esac

# Set path if path not set (if called from /etc/rc)
case ${PATH} in
"") PATH=/bin:/usr/bin:/etc
export PATH ;;
esac

cat ${ORATAB} | while read LINE
do
case ${LINE} in
\#*) #comment-line in oratab
;;
*)
# Proceed only if third field is 'Y'.
if [ "`echo $LINE | awk -F: '{print $3}' -`" = "Y" ] ; then
ORACLE_SID=`echo $LINE | awk -F: '{print $1}' -`
if [ "${ORACLE_SID}" != '*' ] ; then
APPSUSER=`grep $ORACLE_SID /home/oraoper/bin/apps.pwd | awk '{print $5} '`
echo $ORACLE_SID " " $APPSUSER
if [[ -n $APPSUSER ]]
then
typeset -u APPSNODE=`grep $ORACLE_SID /home/oraoper/bin/apps.pwd | awk '{print $6} '`
typeset -u NODE=`uname -n`
/opt/sudo/bin/sudo -u $APPSUSER ssh $APPSUSER@$APPSNODE ls -l
fi
fi
fi
;;
esac
done

==============================================

The first time it hits an SID that requires the call to sudo and ssh, it completes the sudo and ssh, but when it returns to the top of the while loop, it exits, even though there are still lines to process.

Any suggestions?
7 REPLIES 7
John Dvorchak
Honored Contributor

Re: while loop exits after call to ssh

ssh exits with the exit status of the command that is run. Is it possible that if all files are not listed with the ls -l that the exit status of ls is >0 and even though ssh exits, it is passing the non-zero exit status to the script.

Make sure of the permissions of the directory and files you are listing.

man ssh
If it has wheels or a skirt, you can't afford it.
Bharat Katkar
Honored Contributor

Re: while loop exits after call to ssh

Not sure about this but still can you check if this works for you:
Instead of "cat ${ORATAB} | while read LINE"
use
"cat `echo $ORATAB` | while read LINE"

i have used back quotes for echo oratab.

Regards,
You need to know a lot to actually know how little you know
Kevin Nikiforuk
Valued Contributor

Re: while loop exits after call to ssh

I've checked the return code of the sudo and it is zero.

I also tried the cat `echo ${ORATAB}` and that didn't make a difference.
Alex Lavrov.
Honored Contributor

Re: while loop exits after call to ssh

IT happens because you have a pipe there and ssh (and there some more commands) that break this pipe and the loop ends. It's a known issue.

You must use "-f" or "-n" option of ssh:

-f Requests ssh to go to background just before command execution.
This is useful if ssh is going to ask for passwords or
passphrases, but the user wants it in the background. This
implies -n. The recommended way to start X11 programs at a
remote site is with something like ssh -f host xterm.


-n Redirects stdin from /dev/null (actually, prevents reading from
stdin). This must be used when ssh is run in the background. A
common trick is to use this to run X11 programs on a remote
machine. For example, ssh -n shadows.cs.hut.fi emacs & will
start an emacs on shadows.cs.hut.fi, and the X11 connection will
be automatically forwarded over an encrypted channel. The ssh
program will be put in the background. (This does not work if
ssh needs to ask for a password or passphrase; see also the -f
option.)


Alex.
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Victor Fridyev
Honored Contributor

Re: while loop exits after call to ssh

Hi,

I agree with Alex, -n resolves the problem

HTH
Entities are not to be multiplied beyond necessity - RTFM
Kevin Nikiforuk
Valued Contributor

Re: while loop exits after call to ssh

I've done a quick test and it looks good. I'll foward it on to the DBAs and see if it works. I had noticed those flags in the man page, but didn't know they'd apply to me.
Steven E. Protter
Exalted Contributor

Re: while loop exits after call to ssh

Clearly there are some very good answers in this thread. So much so, that I bookmarked it.

Please take the time to assign points to all answers and high points to the answers that magically solved the problem.

From: the peanut gallery.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com