title: Run SQL query in shell script then sends output to email
body: <p>I need to execute a simple select count query in our oracle db. But this takes several hrs to complete. I want to do it using shell script so that I don't need to monitor every now and then if the query is still running  and want to send the output in my email since I only need the counts</p>
  
  <p>I'm doing it in a remote server and don't have admin rights to disable the time limit before going disconnected.</p>
  
  <p>Thank you in advance for the help.</p>
  
  <p>Here is the initial code that I write.</p>
  
  <pre><code>     #!/bin/ksh
  ######################################################################
  # File Name  : counts.sh
  # Created    : 2019/27/19
  # Author     :
  #-----------------------------------------------------------------------
  # $Revision: 1.0 $
  # $Date: 2019/01/27 08:00:00 $
  #-----------------------------------------------------------------------
  # Modification History:
  # No      Date       Author         Note
  #
  #-----------------------------------------------------------------------
  #
  #
  ########################################################################
  DB_USERNAME=$1
  DB_PASSWORD=$2
  DB_DBASE=$3
  
  #------------------------------------------------------------
  # Start Log
  #------------------------------------------------------------
  DB_CONN=${DB_USERNAME}/${DB_PASSWORD}@${DB_DBASE}
  LOG_DATETIME=`date +%Y-%m-%d:%H:%M:%S`
  LOG_FILE=/tmp/$LOG_DATETIME.log
  EGREP=/bin/egrep
  ORA_ERR_STR=ORA
  #==Start Table Space ========================================
  sqlplus -S ${DB_CONN} &gt; $LOG_FILE &lt;&lt; ORAEND
  
  set serveroutput on;
  set feedback off
  SPOOL /tmp/counts.txt
  PROMPT COUNTS
  
  SELECT COUNT(*) FROM schema.db;
  
  spool off;
  ORAEND
  
  
  mailx -s "Counts on $DB_DBASE is"  user@gmail.com &lt; /tmp/counts.txt     
  #===end of script=========#
  </code></pre>
  
  <p>I will call the shell script using nohup command.<br>
  Calling script: nohup ksh counts.sh user password DB &amp; <br></p>