版本: Linux version 2.6.18-194.el5
Oracle Release 10.2.0.5.0
使用工具:shell 脚本
mutt 邮件代理
sendmail
crontab 自动任务处理
一 shell脚本(生成html文件,mutt发送)
1 生成html文件
利用行列转换的sql语句,将sys用户下的tb表(表-1)形式转化为表-2,并生成html1.html文件
表-1
使用的select语句(行列转换)
select name,sum(decode(course,'chinese',score,0)) "chinese",sum(decode(course,'math',score,0)) "math",sum(decode(course,'physics',score,0)) "physics",sum(score) "totle",round(avg(score),2) "avg" from tb group by name;
生成html文件
#设置生成表格式
set pagesize 0
set term off
set feedback off
set linesize 1000
set trimspool on
set term off verify off feedback off pagesize 999
#开启生成html文件
set markup html on entmap on spool on head "
set echo off
spool /home/oracle/html1.html replace
select name,sum(decode(course,'chinese',score,0)) "chinese",sum(decode(course,'math',score,0)) "math",sum(decode(course,'physics',score,0)) "physics",su m(score) totle,round(avg(score),2) avg from tb group by name;
spool off
set markup html off
set echo on
2 使用mutt发送
echo cklovely@vip.qq.com > /tmp/user.txt --需要发送的邮件
echo “test”|mutt -s “subject name” -a /home/oracle/html1.html `cat /tmp/user.txt`
说明:-s 邮件的题目 -a 添加附件 最后是添加发送的邮箱地址
注意:此处默认发件箱是本机 root@localhost.localdomain,而值得一提的是,必须要在sendmail运行的行况下才可以发送
a 关于sendmail启动缓慢的问题
这是个比较常见的问题,出现这个问题的根本原因是主机名没有分配好
解决办法:
我们可以修改系统配置文件/etc/hosts 和/etc/resolv.conf
让sendmial绕过查询远程主机,这里给出一种最简单的方法,给主机设置一个别名
修改/etc/hosts ,未修改之前
127.0.0.1 localhost.localdomain localhost
修改成
127.0.0.1 localhost.localdomain localhost cklovely
b 如果不需要从 root@localhost.localdomain 中发送邮件,可以选择从哪个邮箱发送,在/etc/Muttrc中添加一行
my_hdr from: ckQyanqi@126.com
3 完成html.sh脚本
cat /home/oracle/html.sh
#!/bin/bash
source ~oracle/.bash_profile
$ORACLE_HOME/bin/sqlplus -s "/ as sysdba" <
set pagesize 0
set term off
set feedback off
set linesize 1000
set trimspool on
set term off verify off feedback off pagesize 999
set markup html on entmap on spool on head "
set echo off
spool /home/oracle/html1.html replace
select name,sum(decode(course,'chinese',score,0)) "chinese",sum(decode(course,'math',score,0)) "math",sum(decode(course,'physics',score,0)) "physics",sum(score) totle,round(avg(score),2) avg from tb group by name;
spool off
set markup html off
set echo on
E
echo cklovely@vip.qq.com > /tmp/user.txt
echo "test"|mutt -s "Grade of Students" -a /home/oracle/html1.html `cat /tmp/user.txt`
二 系统自动按时发送
定义时间让系统自动运行脚本的方法很多
而在linux系统中经常使用的要数crontab了。
(其他的方法有:oracle中的dbms_jobs,还有就是dbms_scheduler)
/etc/cron.deny:
將不可以使用 crontab 的帳號寫入其中,若未記錄到這個檔案當中的使用者,就可以使用 crontab 。
运行crontab有2种方法:
1 直接用crontab命令
當使用者使用 crontab 這個指令來建立工作排程之後,該項工作就會被紀錄到 /var/spool/cron/ 裡面去了,而且是以帳號來作為判別的。另外, cron 執行的每一項工作都會被紀錄到 /var/log/cron 這個登錄檔中,所以囉,如果你的 Linux 不知道有否被植入木馬時,也可以搜尋一下 /var/log/cron 這個登錄檔呢!
Crontab语法:
crontab [-u username] [-l|-e|-r]
croutab -e (默认username为root)打开/var/spool/cron/root 编辑
croutab -l (默认 username 为root)查看root用户下的自动任务
croutab -r (默认 username 为root)删除root下所有的自动任务
编辑内容:
* * * * * command
2 系统设定文件:/etc/crontab 必须是root用户设定
* MAILTO=root:
這個項目是說,當 /etc/crontab 這個檔案中的例行性工作的指令發生錯誤時,或者是該工作的執行結果有 STDOUT/STDERR 時,會將錯誤訊息或者是螢幕顯示的訊息傳給誰
例如 MAILTO=cklovely@vip.qq.com
* PATH=....
这里就是输入执行命令的搜索路径,使用预设定路径已经足够了
* 01 * * * * root run-parts /etc/cron.hourly;
注意:但是在五个时间后面