bash 如何从unix脚本发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3831871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send an email from unix script
提问by ErAB
#! /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 ?
1.首先,我如何安排脚本每 1 小时运行一次?
2.Secondly, the requirement is to send an email on condition such as :
2.其次,要求在以下条件下发送电子邮件:
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].
如果 total_records < 1,则应向 [email protected] 发送 UP ALERT 通知电子邮件。
一旦 total_records 大于 1,则再次向 [email protected] 发送 DOWN ALERT 通知电子邮件。
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.
注意:直到 total_records > 1,没有上面的事情(pt.2)要遵循。只有当 total_records < 1 时,我们才需要执行步骤 2。
Here, total_records represents transactions, so it will change in every hour (as the tr.TIMESTAMPsignifies). tr represents transaction table.
这里,total_records 代表交易,所以它会每小时发生变化(如tr.TIMESTAMP符号所示)。tr 代表事务表。
采纳答案by dogbane
To run the script every hour at the 0 minute mark, add an entry to your crontab like this:
要在 0 分钟标记处每小时运行一次脚本,请在您的 crontab 中添加一个条目,如下所示:
- Type
crontab -eto edit your crontab. Then add the following line and save the crontab:
0 * * * * myscript.sh > myscript.log
- 键入
crontab -e以编辑您的 crontab。 然后添加以下行并保存 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:
现在,您需要根据自己的情况确定是否应该发送电子邮件。这样做的一种方法是将 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
Then apply your conditions to sql.outby using a grepor wccommand and send an email using mailx(if installed) or sendmail:
然后sql.out使用grep或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
回答by jyz
1) To run a command every hour just edit your user crontab and add:0 * * * * command
1)要每小时运行一个命令,只需编辑您的用户 crontab 并添加:0 * * * * command
2) This could be done in a lot of ways. Is the UNIX mail server configured?
2)这可以通过多种方式完成。是否配置了 UNIX 邮件服务器?
回答by ConcernedOfTunbridgeWells
For (1) look at the man page for cron(1). This will do what you want.
对于 (1),请查看 cron(1) 的手册页。这会做你想做的。
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.
对于 (2),您需要从 Oracle 内部发出电子邮件(请参阅 DBMS_MAIL 包)或将 SQLPlus 的输出重定向到文件并在 shell 脚本中处理该文件。有关如何执行此操作的详细信息,请参阅 mail(1) 命令的文档。
回答by ConcernedOfTunbridgeWells
Take a look at the manpages of crond and sendmail.
查看 crond 和 sendmail 的联机帮助页。

