Catching exception of commands executed under ssh and su

Hello experts,

I have one wrapper script which runs on server104, the purpose of the script is to ssh on server109; perform su to application user; and execute exports script.

I want wrapper script to stop executing further if my exports script comes out with exit 1. My current setup keeps on running even if the export script fails at some point, I believe this is because I am running it under ssh command since it returns exit 0 to my current shell.

Script looks like this:

ssh -i /opt/apps/lib/keys/nfs_appsadm_key $apps_USER@$SOURCE_MACHINE_NAME <<EOT 

echo $SOURCE_SW_NAME 
sudo su - $SOURCE_SW_USER <<EOT 
  
        /opt/apps/home/apps/bladelogic_scripts/export.sh -t $TICKET_NUMBER -f $LIST_FILE 
                        
exit 
EOT 
echo Export of source apps complete  | tee  -a $BATCH_PROMO_LOG 
  
echo "####Copying the exported content from $EXP_DIR on $SOURCE_MACHINE_NAME to $IMP_DIR on localhost.####" 

scp -i /opt/apps/lib/keys/nfs_appsadm_key -r $apps_USER@${SOURCE_MACHINE_NAME}:${EXP_XFR_DIR}/*.* $IMP_DIR 
echo "Changing permission of recently modified files in $IMP_DIR" 
cd $IMP_DIR 
find . -type f -mtime -1 -exec chmod 777 {} \; 
#chmod -R 777 $IMP_DIR/*.txt
#chmod -R 777 $IMP_DIR/"$TICKET_NUMBER"_detail.txt 

I can write the exit code of exports script in a temp file and then scp it to server109, check its existence and code wrapper to come out if it exists but feel I am complicating it, any easy solutions to catch the exception and stop execution of wrapper if exports script throws error?

Any feedback is appreciated.

Thank You.

You might run it more on an expect-like basis, run '(feerer-subshell)|ssh whatevery su -c ksh >logfile 2>&1', feed it commands, check the output for positive indications of success, send more commands. If you redirect output to a local flat file, you can interrogate that file in the feeder subshell to see if it ends in the right stuff.

You could run it with expect, too.

Worked ok here when I removed the exit command and used different EOF tags for ssh and sudo here-docs:

[chubler@myhost ~]$ ssh remotehost cat /tmp/export.sh
echo "All ok"
exit 3
[chubler@myhost ~]$ ssh remotehost <<EOF
>    echo "Starting"
>    sudo su - appuser <<su_EOF
>        /tmp/export.sh
> su_EOF
> EOF
Pseudo-terminal will not be allocated because stdin is not a terminal.
Starting
All ok
[chubler@myhost ~]$ echo $?
3
1 Like

Hi, I tried the way mentioned above and removed exit command from the script, it still gives comes with exit code 0 and the execution processes further:

ssh -i /opt/apps/lib/keys/nfs_appsadm_key $apps_USER@$SOURCE_MACHINE_NAME <<EOF 
sleep 2 
echo $SOURCE_SW_NAME 
sleep 2 
sudo su - $SOURCE_SW_USER <<su_EOF 
  
        /opt/apps/home/apps/scripts/procedure_export.sh -t $TICKET_NUMBER -f $PROCEDURE_LIST_FILE 
 
su_EOF 
EOF 
sleep 2 
echo $? 
echo Export of source procedures complete  | tee  -a $BATCH_PROMO_LOG 

Here's the log of above utility:

Info May 9, 2013 10:06:43 PM [stdout: 2] Parsing input file /opt/apps/home/apps/bladelogic_scripts/procedure_name.txt for duplicate values at first column
2
Info May 9, 2013 10:06:44 PM [stdout: 2] ####Error - Duplicate procedure entries exists in the /opt/apps/home/apps/scripts/procedure_name.txt, please remove duplicate values from input file, exiting now.#### avikal is duplicate
2
Info May 9, 2013 10:06:46 PM [stdout: 2] 0
2
Info May 9, 2013 10:06:46 PM [stdout: 2] Export of source procedures complete
2

The OS is solaris, and my used case is to parse the file for duplicate values at column first in export script, if they exists execution should stop completely.

Please advise.

---------- Post updated at 08:59 AM ---------- Previous update was at 04:17 AM ----------

Nevermind guys, sleep before catching the exception was the issue. Script is now working as expected. Apologies and thanks for the inputs.