Solved

Why do I get kicked out of Unix session after running a Unix script?

Posted on 2014-09-09
6
259 Views
Last Modified: 2014-09-10
I have a bash script that runs a few SQL Plus scripts, does a SFTP process and creates two differing log files, one for the SFTP process and one for everything else.  The script completes everything successfully, writes the log files and nothing seems to be missing from the process except after running the script from the bash prompt, it asks me for a password to log back in.  Not sure why it does this.  It doesn't bother me except I don't think it should go into production like this.  Also, I took out every single exit and it doesn't seem to change anything!

The line in the shell script is #!/bin/bash.  And I run it from the command prompt as ./xyz.sh

Please let me know what ideas you may have!  Thanks!
Abbi
0
Comment
Question by:Bodhi108
  • 3
  • 2
6 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 300 total points
ID: 40313108
Which kind of login do you see? Oracle? Shell?

Is there a "login username" statement somewhere in your script? Or an Oracle "Connect ..." statement?

Is it really a login prompt? Could it come from sudo or sftp or something of that sort?

You could add "set -xv" near the top of the script in order to get some debug info.
0
 
LVL 9

Assisted Solution

by:Carlos Ijalba
Carlos Ijalba earned 200 total points
ID: 40314071
Hi Bodhi108,

Without seeing the script, it will be very difficult to guess why.

As woolmilkporc suggests it could be because it jumps to another host with sftp, rsh, ssh, etc, or maybe that the prompt is a connection to a DB, in which case, the script should be changed in order to login to the DB without asking for password.

Same goes for sudo, it can be modified to run scripts without asking for passwords.

Also check that the script doesn't have the awful "exit 0" or any other exit statement at the end.
0
 

Author Comment

by:Bodhi108
ID: 40315588
I'm a advanced beginner shell programmer so I may not give you the perfect answers so I've included a shell script in which I modified the names for security reasons.  I also included a screen print to show you how it is being run and the password prompt.  

The shell script was developed by someone else who retired and I took it over.  I was given it to make sure there weren't any issues before moving it into Test.

Answering your questions to the best of my ability:
Login - shell
login to Oracle uses wallet - sqlplus  -silent /@abcdev
login prompt? - I believe it is logging me back on the unix box
debugging - I put in -x at the beginning and there are no errors and everything gets processed even the last line of code.
SFTP - uses certificates and it was working since file is being SFTP'd and does not need password.
I don't believe there are any sudo's going on in the script.

Screen Print attached:

Script below with modified names:
#!/bin/bash

# File name: yyy
# Author(s): xxxx
#
# Revisions:
# ITR#        Date        Author             Description
# ----------- ----------  -----------------  ------------------------------
# xyz        20FEB2014   Doug xxx        Initial creation
#
# Overview: This is the driver script for the xxx  Extract Process.
#           This process reads a flat file, creates a csv file for the abc dept  and then SFTP’d to abc dept.
#            The last step sends email to a select group.

# Variable set up

JOB_NAME=$(basename $0)
#DATE_YYMD=$(date '+%Y-%m-%d')
#DATE_YYMD=$(date '+%Y-%m-%d')
DATE_YYMDS=$(date '+%Y%m%d%S')

NOTIFY_LIST="$(abc /def/ghi/xyz.maillist)"
SFTP_LOG=/abc/def/ghi/${JOB_NAME}_$DATE_YYMDS.sftp
touch $SFTP_LOG
LOG=/abc/def/ghi//${JOB_NAME}_$DATE_YYMDS.log
touch $LOG

# Command paths

MAILER=/usr/abc/Mail
SFTP=/usr/bin/sftp

# Define Functions

# The EOJ Function is called to End the Job.  It performs final functions including
# Emailing the Log file.

function EOJ
{
   echo " " >> $LOG
   echo "$(date +'%Y/%m/%d %T') $JOB_NAME Ended, return code = $RETCODE" >> $LOG
   if [ ${RETCODE} -ne 0 ]
      then
         $MAILER -s "RUN LOG: $JOB_NAME FAILED"                 $NOTIFY_LIST < $LOG
      else
         $MAILER -s "RUN LOG: $JOB_NAME COMPLETED SUCCESSFULLY" $NOTIFY_LIST < $LOG
   fi
# may need to add next line --debugging exit
#   exit $RETCODE
}

# Mainline Code

RETCODE=0

echo "$(date +'%Y/%m/%d %T') $JOB_NAME Started" > $LOG
echo "$(date +'%Y/%m/%d %T') Connecting to Oracle via sqlplus" >> $LOG

# Get the job_id from the job_app table which will be used throughout this process.
# Declare the variable as Integer.

typeset -i p_job_id

#  sqlplus  -silent /@abcdev << EOSQLPLUS  >> $LOG
p_job_id=`
   sqlplus  -silent /@abcdev << EOSQLPLUS
      @get-job-id.sql
EOSQLPLUS`

SQL_RETURN_CODE=$?

echo "" >> $LOG

if [ ${SQL_RETURN_CODE} -ne 0 ]
then
   echo "$(date +'%Y/%m/%d %T') SQL script Failed, return code = $SQL_RETURN_CODE" >> $LOG
   RETCODE=$SQL_RETURN_CODE
   EOJ
else
   echo "$(date +'%Y/%m/%d %T') SQL script completed successfully, return code = $SQL_RETURN_CODE" >> $LOG
fi

# I'll pass APP_NAME to the SQL script and use it as a key to the app_log Log Framework table.  The following
# statement pulls the first 4 characters from the Job Name which are also the App Name.  I will also pass
# along the job_id which is needed as a key field to the app_log table.

APP_NAME=$(echo $JOB_NAME|awk '{print substr($0,1,4)}')

# abcdev is a tnsnames alias to an entry in an Oracle Wallet

sqlplus  -silent /@abcdev << EOSQLPLUS
   @xyz.sql $APP_NAME $LOG $p_job_id
EOSQLPLUS

SQL_RETURN_CODE=$?

echo "" >> $LOG

if [ ${SQL_RETURN_CODE} -ne 0 ]
then
   echo "$(date +'%Y/%m/%d %T') SQL script Failed, return code = $SQL_RETURN_CODE" >> $LOG
   RETCODE=$SQL_RETURN_CODE
   EOJ
else
   echo "$(date +'%Y/%m/%d %T') SQL script completed successfully, return code = $SQL_RETURN_CODE" >> $LOG
fi

echo "" >> $LOG
echo "$(date +'%Y/%m/%d %T') Begin Secure File Transfer" >> $LOG

#$SFTP its-prod-xfer-1 << SFTPEOF 1>> $SFTP_LOG 2>> $SFTP_LOG
  $SFTP its-prod-xfer-1 << SFTPEOF 1>> $SFTP_LOG 2> $SFTP_LOG
   put /abc/def/ghi/jkl/xyz.csv
   quit
SFTPEOF

# Save status of and log output from sftp

SFTP_RETURN_CODE=$?
cat $SFTP_LOG >> $LOG

# Check the sftp output for key phrases from failure messages in case
# one of the commands had an issue but the overall status was OK

echo "$(date +'%Y/%m/%d %T') End   Secure File Transfer"     >> $LOG
echo "" >> $LOG
echo "$(date +'%Y/%m/%d %T') Check sftp output for failures" >> $LOG
 
egrep -i 'failed|rejected|unable|No such file or dir|Permission denied|no address|Login incorrect|Broken pipe|Lost connection|Not connected|usage|not found' $SFTP_LOG 1>> $LOG 2>> $LOG

EGREP_STAT=$?

if [[ $EGREP_STAT -eq 0 || $SFTP_RETURN_CODE -ne 0 ]]
   then
      echo "$(date +'%Y/%m/%d %T') Transfer FAILED, see above message(s)" >> $LOG
      RETCODE=8
      EOJ
   else
      echo "$(date +'%Y/%m/%d %T') Transfer completed Successfully!" >> $LOG
fi

EOJ

# may need to add next line --debugging exit
#exit $RETCODE
Capture.PNG
ScreenPrint.JPG
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40315629
The culprit could be here within  "xyz.sql" (not very probable, but who knows?):

sqlplus  -silent /@abcdev << EOSQLPLUS
   @xyz.sql $APP_NAME $LOG $p_job_id
EOSQLPLUS

Please examine this procedure. Any hints?

Maybe it's here:

MAILER=/usr/abc/Mail

doesn't seem to be a standard mail client program. What is it? Since this mailer is called by EOJ at the very end it seems suspicious.
0
 

Author Comment

by:Bodhi108
ID: 40315686
hmmmm....yes, I will look at /usr/abc/MAIL  Since everything runs fine and this is the last thing, it could be the culprit.  Thanks!
0
 

Author Comment

by:Bodhi108
ID: 40315942
Mail points to mailx so that is not the issue.

Found out the issue.  I could have found out the issue a lot faster if I entered an incorrect password in 3 times!  Kicking myself on that.

My authenticating keys are not set up on the servers.  So after running the job, it was asking me for the password to complete the SFTP.  If I were to enter a correct password after the 'job ran', the SFTP would happen at that point.  If I didn't enter in a password the SFTP would not happen.  So I had help in generating the RSA keys and now it no longer asks me for a password. It probably didn't happen with the previous owner of this code since his environment was already set up with the authenticating keys. I'm a new employee so not everything was set up in my environment.

Thanks for your help.  I'll give out the points now!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Preface There are many applications where some computing systems need have their system clocks running synchronized within a small margin and eventually need to be in sync with the global time. There are different solutions for this, i.e. the W3…
This video shows how to recover a database from a user managed backup
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now