1838230 Members
5982 Online
110125 Solutions
New Discussion

Re: rsh and return code

 
Mike_Ca Li
Regular Advisor

rsh and return code

Hi everyone,

Someone posted ssh and return code and did not get much replies. I'm looking for rsh and return code when between Servers.
Does anyone know how to trap rsh return code? For example, on server A, rsh to server B and run command X (which run non-zero on error or when ran directly on Server B). I would like to trap the return code of command X when rshed from Server A. Currently rsh -l username ServerB command
always return 0

The long way to this would be to trap the return code to a file when running command X on server B, then run a rcp to grab that file from server A. Any ideas?

Thanks in advance for your time.
15 REPLIES 15
Pat Lieberg
Valued Contributor

Re: rsh and return code

You could use the conditional || on the command line to return $? if the command fails.

For example:

rsh remotehost -n cat /tmp/file || echo $?

Shouldn't be too hard to capture the code that is returned.
RAC_1
Honored Contributor

Re: rsh and return code

Do it as follows.

rsh remotehost 'cat /tmp/file;echo $?'

Anil
There is no substitute to HARDWORK
Mike_Ca Li
Regular Advisor

Re: rsh and return code

We have this scenerio:
"rsh -l username ServerB /home/username/bin/programX"; echo$?
It will always return 0 because the rsh will run successfully.

in programX, I have codes to check for running of a backup program on ServerB. If backup is success, 0 is returned, but if failure, 99 is returned. If programX is ran on ServerB when backup failed, echo$? gives 99, but when ran with rsh, echo$? gives 0. How does return code is propagated via rsh ?
I tried with -n still did not work. Please advise.

Pat Lieberg
Valued Contributor

Re: rsh and return code

You need to include the ;echo $? inside the quotes of the command passed to rsh as in RAC's example above. Otherwise you are getting the return code of the rsh and not the return code of the remote program.
Mike_Ca Li
Regular Advisor

Re: rsh and return code

Hi Pat:
I don't think that double quotes are the issue.
rsh -l username ServerB /home/username/programX; echo $?
returns 0
Pat Lieberg
Valued Contributor

Re: rsh and return code

I've tried it with both ssh and remsh and it seems to work fine. Not sure why you always see a 0 result.
Pat Lieberg
Valued Contributor

Re: rsh and return code

On second thought, there's only two things running here, your rsh and the remote command. The zero result must be coming from the rsh if you know the remote command is failing. That means the shell is running your echo $? on the local system and not remotely.

Enclosing the command in single quotes will guarantee the echo is run remotely.

If I do:

remsh hostname 'cat blah';echo $?

the result is:
cat: Cannot open blah: No such file or directory
0

However, if I do:

remsh hostname 'cat blah;echo $?'

the result is:

cat: Cannot open blah: No such file or directory
2

Sorry to keep on the quotes but I can't think of what else would cause your return code to always be 0.
Mike_Ca Li
Regular Advisor

Re: rsh and return code

Hi Pat:

You got something there with the single quote:
remsh hostname 'cat blah;echo $?'

the result is:

cat: Cannot open blah: No such file or directory
2

But how to do something like below:
remsh hostname 'cat blah; result=$?;export result'
if [ $result -eq 2 ]
then
pageout
fi
Currently [ $result -eq 2 ] is failing because the result variable is not being returned to the shell. Any more insights? I'll assign points. Thank you.
Pat Lieberg
Valued Contributor

Re: rsh and return code

Your best bet is to capture the result of the command to a variable:

result=$(remsh hostname 'cat blah 2>/dev/null; result=$?')
if [ $result -eq 2 ]
then
pageout
fi

The 2>/dev/null makes it so any error messages are not captured and the $result variable will get just the return code number.
Mike_Ca Li
Regular Advisor

Re: rsh and return code

If one tests this following, result is not set to anything.

result=$(remsh hostname 'cat /tmp/blah 2>/dev/null; result=$?')
if [ $result -eq 2 ]
then
pageout
fi
Andrew Merritt_2
Honored Contributor

Re: rsh and return code

You need to return the result from the command; any assignment you do will be done remotely.

Something like this should work:

result=$(remsh hostname 'cat /tmp/blah 2>/dev/null; echo $?')

Pat Lieberg
Valued Contributor

Re: rsh and return code

oops, typo on my part:

result=$(remsh hostname 'cat /tmp/blah 2>/dev/null;echo $?')

I had copied your code and forgot to change that to an echo statement on the end.
RAC_1
Honored Contributor

Re: rsh and return code

Mike, your code is not right.
result=$(remsh hostname 'cat /tmp/blah 2>/dev/null; result=$?')
if [ $result -eq 2 ]
then
pageout
fi

Will just return remsh return code and not the return code of cat command which you excuted on remote host. Do it as follows.

remsh hostname -l user_name 'cat xyz 2>/dev/null;echo $? > /tmp/status' ## Put the return status in a file
result=$(remsh hostname 'cat /tmp/status')
if [ $result -eq 2 ]
then
pageout
fi
There is no substitute to HARDWORK
Pat Lieberg
Valued Contributor

Re: rsh and return code

Mike's code is correct. The single quotes around the command passed to remsh guarantee the return code comes from the remote system and not the local remsh command.

I've tested it.
Mike_Ca Li
Regular Advisor

Re: rsh and return code

thanks Pat RAC, Andrew. Working now.