如何从 unix 脚本发送电子邮件

发布于 2024-09-25 17:32:05 字数 1358 浏览 4 评论 0原文

#! /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

新人笑 2024-10-02 17:32:05

要在每小时 0 分钟标记运行该脚本,请向您的 crontab 添加一个条目,如下所示:

  • 输入 crontab -e 编辑您的 crontab。
  • 然后添加以下行并保存 crontab:

    0 * * * * myscript.sh > myscript.log
    

现在,您需要根据您的情况确定是否应该发送电子邮件。实现此目的的一种方法是将 SQL 命令的输出写入如下文件:

#! /bin/bash
`sqlplus -s <username>/<passwd>@dbname` << EOF > sql.out  
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

然后使用 grep将条件应用到 sql.out >wc 命令并使用 mailx(如果已安装)或 sendmail 发送电子邮件:

if grep -q something sql.out
then
    # send email containing the output of the SQL statement
    cat sql.out | mailx -s "UP ALERT" [email protected]
fi

#delete sql.out
rm sql.out

To run the script every hour at the 0 minute mark, add an entry to your crontab like this:

  • Type crontab -e to edit your crontab.
  • Then add the following line and save the crontab:

    0 * * * * myscript.sh > myscript.log
    

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:

#! /bin/bash
`sqlplus -s <username>/<passwd>@dbname` << EOF > sql.out  
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

Then apply your conditions to sql.out by using a grep or wc command and send an email using mailx (if installed) or sendmail:

if grep -q something sql.out
then
    # send email containing the output of the SQL statement
    cat sql.out | mailx -s "UP ALERT" [email protected]
fi

#delete sql.out
rm sql.out
水水月牙 2024-10-02 17:32:05

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?

温柔嚣张 2024-10-02 17:32:05

对于 (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.

少女的英雄梦 2024-10-02 17:32:05

查看 crond 和 sendmail 的联机帮助页。

Take a look at the manpages of crond and sendmail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文