简体   繁体   English

在Shell脚本中运行SQL查询,然后将输出发送到电子邮件

[英]Run SQL query in shell script then sends output to email

I need to execute a simple select count query in our oracle db. 我需要在我们的oracle数据库中执行一个简单的选择计数查询。 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 我想使用Shell脚本来执行此操作,因此我不需要时常监视查询是否仍在运行,并且由于我只需要计数,因此想在我的电子邮件中发送输出

I'm doing it in a remote server and don't have admin rights to disable the time limit before going disconnected. 我正在远程服务器上进行操作,并且没有管理员权限在断开连接之前禁用时间限制。

Thank you in advance for the help. 预先感谢您的帮助。

Here is the initial code that I write. 这是我编写的初始代码。

     #!/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} > $LOG_FILE << 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 < /tmp/counts.txt     
#===end of script=========#

I will call the shell script using nohup command. 我将使用nohup命令调用shell脚本。
Calling script: nohup ksh counts.sh user password DB & 调用脚本:nohup ksh counts.sh用户密码DB&

You never said what is result of that query. 您从未说过该查询的结果。 Yes, it counts something, but - how does the output look like? 是的,它很重要 ,但是-输出看起来如何? One value? 一个值? One column in several rows? 多行中的一列? Many columns in one row? 一行中有很多列? Many columns in even more rows? 多列甚至更多行?

As the query itself takes hours to complete, perhaps you'd want to be able to check the result sorted by this or that , perform some calculations, etc. - if that's so, maybe the simplest option is to 由于查询本身需要花费数小时才能完成,因此您可能希望能够检查按thisthat排序的结果,进行一些计算等。-如果是这样,也许最简单的选择是

  • create a table that contains columns returned by that query 创建一个包含该查询返回的列的表
  • write a stored procedure that inserts the result into the table 编写一个存储过程,将结果插入表中
  • at the end of the procedure, use UTL_MAIL (or UTL_SMTP ) and send yourself a mail, simply saying that processing is over 在该过程结束时,请使用UTL_MAIL (或UTL_SMTP )并向自己发送一封邮件,只是说处理已结束
  • connect to the database and select * from the table in order to review the result 连接到数据库并从表中选择*以查看结果
  • if the query result is simple, you could put it into the e-mail message body 如果查询结果很简单,则可以将其放入电子邮件正文中

Another option is to actually create the output file (using UTL_FILE ) and send it by mail as an attachment. 另一个选择是实际创建输出文件(使用UTL_FILE )并通过邮件将其作为附件发送。

Or, if you want to do it from the operating system level, you should still create the output file and use some tool that enables you to send mail. 或者,如果要从操作系统级别执行此操作,则仍应创建输出文件并使用一些使您能够发送邮件的工具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM