如何从 unix 脚本发送电子邮件
#! /bin/bash
`sqlplus -s <username>/<passwd>@dbname` << EOF
set echo on
set pagesize 0
set verify off
set lines 32000
set trimspool on
set feedback off
`SELECT starts_with, SUM (total_records) total_records
FROM (SELECT ID,
(CASE WHEN ID LIKE '2%' THEN '2____'
WHEN ID LIKE '3%' THEN '3____'
WHEN ID LIKE '99%' THEN '99____'
END
) starts_with,
total_records
FROM tr
where ( id like '2%' or id like '3%' or id like '99%'))
WHERE tr.TIMESTAMP > SYSDATE - 75 / 1440
AND tr.TIMESTAMP <= SYSDATE - 15 / 1440
GROUP BY starts_with;
`
exit;
EOF
1.首先,我如何安排脚本每 1 小时运行一次?
2.其次,要求发送电子邮件的条件例如:
iftotal_records<; 1,然后应将 UP ALERT 通知电子邮件发送至 [email protected] .
一旦 Total_records 大于 1,则会再次将向下警报通知电子邮件发送至 [电子邮件受保护]。
注意:直到total_records > 1,没有上述的事情(pt.2)可以遵循。仅当total_records < 时1,我们需要执行步骤2。
这里,total_records代表交易,因此它每小时都会发生变化(如tr.TIMESTAMP
所示)。 tr代表事务表。
#! /bin/bash
`sqlplus -s <username>/<passwd>@dbname` << EOF
set echo on
set pagesize 0
set verify off
set lines 32000
set trimspool on
set feedback off
`SELECT starts_with, SUM (total_records) total_records
FROM (SELECT ID,
(CASE WHEN ID LIKE '2%' THEN '2____'
WHEN ID LIKE '3%' THEN '3____'
WHEN ID LIKE '99%' THEN '99____'
END
) starts_with,
total_records
FROM tr
where ( id like '2%' or id like '3%' or id like '99%'))
WHERE tr.TIMESTAMP > SYSDATE - 75 / 1440
AND tr.TIMESTAMP <= SYSDATE - 15 / 1440
GROUP BY starts_with;
`
exit;
EOF
1.Firstly, how can i schedule the script to run after every 1 hr ?
2.Secondly, the requirement is to send an email on condition such as :
if total_records < 1, then an UP ALERT notification email should be send to [email protected].
And as soon as the total_records gets greater than 1, then again DOWN ALERT notification email is send to [email protected].
NOTE : Till total_records > 1, no such above thing (pt.2) to be followed. Only, when it total_records < 1, we need to follow step 2.
Here, total_records represents transactions, so it will change in every hour (as the tr.TIMESTAMP
signifies). tr represents transaction table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要在每小时 0 分钟标记运行该脚本,请向您的 crontab 添加一个条目,如下所示:
crontab -e
编辑您的 crontab。然后添加以下行并保存 crontab:
现在,您需要根据您的情况确定是否应该发送电子邮件。实现此目的的一种方法是将 SQL 命令的输出写入如下文件:
然后使用
grep
或将条件应用到
命令并使用sql.out
>wcmailx
(如果已安装)或sendmail
发送电子邮件:To run the script every hour at the 0 minute mark, add an entry to your crontab like this:
crontab -e
to edit your crontab.Then add the following line and save the crontab:
Now, you need to work out if you should send an email or not, based on your conditions. One way of doing this, is to write the output of your SQL command to a file like this:
Then apply your conditions to
sql.out
by using agrep
orwc
command and send an email usingmailx
(if installed) orsendmail
:1) 要每小时运行一个命令,只需编辑您的用户 crontab 并添加:
0 * * * * 命令
2) 这可以通过多种方式完成。 UNIX 邮件服务器是否已配置?
1) To run a command every hour just edit your user crontab and add:
0 * * * * command
2) This could be done in a lot of ways. Is the UNIX mail server configured?
对于 (1),请查看 cron(1) 的手册页。这将做你想做的事。
对于 (2),您需要从 Oracle 内部发出电子邮件(请参阅 DBMS_MAIL 包)或将 SQLPlus 的输出重定向到文件并在 shell 脚本中处理该文件。有关如何执行此操作的详细信息,请参阅 mail(1) 命令的文档。
For (1) look at the man page for cron(1). This will do what you want.
For (2) you will need to either issue the emails from within Oracle (see the DBMS_MAIL package) or redirect the output from SQLPlus to a file and process the file in a shell script. See the documentation for the mail(1) command for details on how to do this.
查看 crond 和 sendmail 的联机帮助页。
Take a look at the manpages of crond and sendmail.