通过 shell 脚本调用 sql 过程,如果出错则发送电子邮件
我希望编写一个调用 sql 过程的 shell 脚本(最终通过调度程序运行该脚本),如果该过程给出任何类型的 sql 错误,则将该错误作为电子邮件发送,我希望在脚本中指定该错误。 我该怎么做呢?
编辑:这是我尝试过的。我无法弄清楚如何放置仅在出现错误时发送电子邮件的逻辑。
export ORACLE_HOME=/oracle/app/products/11gr1/db
export ORACLE_SID=CTPP01S1
MAIL_TO="[email protected]"
LOGFILE=./xxx_job.log
exec 2>&1 > $LOGFILE
echo " Job run at: `date`......"
$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' <<EOF
set head on;
set feed on;
set serveroutput on;
set linesize 250;
set pagesize 1000;
exec schema.proc_name;
exit
EOF
echo "Job ended at: `date`"
mailx -s "OLBB Extract Results." $MAIL_TO < $LOGFILE
I wish to write a shell script that calls a sql procedure(ultimately run the script through a scheduler) and if the procedure gives an sql error of any kind, send the error as an email which i wish to specify inside the script.
How can I do it?
EDIT: Here is what I tried. I cannot figure out how to put the logic that send email only if there is error.
export ORACLE_HOME=/oracle/app/products/11gr1/db
export ORACLE_SID=CTPP01S1
MAIL_TO="[email protected]"
LOGFILE=./xxx_job.log
exec 2>&1 > $LOGFILE
echo " Job run at: `date`......"
$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' <<EOF
set head on;
set feed on;
set serveroutput on;
set linesize 250;
set pagesize 1000;
exec schema.proc_name;
exit
EOF
echo "Job ended at: `date`"
mailx -s "OLBB Extract Results." $MAIL_TO < $LOGFILE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想从 Oracle 发送邮件,则 utl_mail 是你的朋友。正如 @Tony 指出的,你没有指定你的平台,但在 Linux 中有 mail 和 Unix 中,您都可以传递执行的输出到。
在任何环境中,您都可以在包装器中执行 SQL,例如 Python(我有偏见)和 邮件 来自那里。
If you want to send the mail from Oracle then utl_mail is your friend. As @Tony noted you didn't specify your platform, but in Linux there's mail and in Unix both of which you can pass the output of your execution to.
In any environment you can execute your SQL in a wrapper such as Python (I'm biased) and mail from that.