Shell脚本使用AIX中的Mailx通过电子邮件发送SQLPlus查询的结果 [英] Shell script to email results of SQLPlus query using Mailx in AIX

查看:434
本文介绍了Shell脚本使用AIX中的Mailx通过电子邮件发送SQLPlus查询的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要的命令。如果我在提示符下执行这些命令,那么一切都会按预期进行。 SQLPlus运行查询,将结果导出到文件中,然后Mailx通过电子邮件将文件发送给我。

I have the commands that I need. If I execute these commands at the prompt, everything works as expected. SQLPlus runs the query, exports the result to a file and then Mailx emails that file to me.

sqlplus username/pwd@instance
SPOOL /home/sadmin/sqlmail/spool.out
set linesize 2000
set wrap off
@/home/sadmin/sqlmail/query.sql
SPOOL OFF
exit
mail -s "Subject" email@address.com < /home/sadmin/sqlmail/spool.out

但是,我不能,因为我一生,弄清楚如何将它们放在.sh文件中,以便我可以对其进行计划。帮帮我!?并预先感谢,我敢肯定这是很愚蠢的。

But, I can not, for the life of me, figure out how to put these in an .sh file so that I can schedule it. Help!? And thanks in advance, I'm sure this is very silly.

推荐答案

使用shell'Here'文档是通常的解决方案,即

Using a shell 'Here' document is the usual solution, i.e.

 cat MyCommand.sh
 #!/bin/bash  # (or ksh, sh, etc)
 PATH=.../path/to/sqlplusdir:${PATH}

sqlplus username/pwd@instance <<EOS
    SPOOL /home/sadmin/sqlmail/spool.out
    set linesize 2000
    set wrap off
    @/home/sadmin/sqlmail/query.sql
    SPOOL OFF
    exit
EOS
mail -s "Subject" email@address.com < /home/sadmin/sqlmail/spool.out

您需要将PATH环境设置为包括

You'll need to set your PATH env to include the path to you sqlplus executable.

然后,您需要一个具有cron工具访问权限的用户ID。您知道如何使用 vi编辑器吗?调用 crontab 时,您将查看当前用户预定的作业,并且需要使用 vi命令来操作该文件。 (可能会覆盖使用哪个编辑器,但不建议使用)

Then you need a userID with access to the cron facility. Do you know how to use the 'vi' editor? When you call crontab, you will be looking at the current users scheduled jobs and you will need to manipulate the file with 'vi' commands. (It may be possible to override which editor to use, but not recommended)

您需要阅读cron的手册页,即 man cron ,您将被称为上述完整脚本。 cron条目将如下所示:

You need to read the man page for cron, i.e. man cron, and you will be call the above a a complete script. A cron entry will look like

59 23 31 12 * { var=x; export var ; myCommand ; } > /tmp/myWorkDir/myCommand.trace 2>&1 

min
   hr
     day
       mon
          (DayOfWeek)

日期/时间的值可以是逗号分隔的列表(0,15,30,45),连字符分隔的范围(4-6 )或*表示所有有效值。

values for date/times can be comma separated lists (0,15,30,45), hyphen separated ranges (4-6) or * to indicate all valid values.

这会捕获从myCommand第一次运行到tmpDir中文件中的所有输出,包括stderr。

This captures any output including stderr, from the 1 time run of myCommand into the file in the tmpDir.

以上的最低版本为

59 23 12 31 * var=x; export var ; myCommand

然后将所有输出发送到用户的本地邮箱。

and then any output is sent to the user's local mailbox.

我希望这会有所帮助。

这篇关于Shell脚本使用AIX中的Mailx通过电子邮件发送SQLPlus查询的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆