search
HomeDatabaseMysql TutorialLinux下Oracle查询结果以html格式定期发送邮件

任务: 每天早上8:00将oracle查询语句的内容变成html的形式,制作一脚本,在指定时间用mutt自动发送到邮箱版本: Linux version

任务: 每天早上8:00将Oracle查询语句的内容变成html的形式,制作一脚本,在指定时间用mutt自动发送到邮箱

版本: 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

NAME       COURSE          SCORE
---------- ---------- ----------
chen       chinese            74
chen       math                 99
chen       physics            88
ck          chinese            78
ck          math                 89
ck          physics            98

表-2
NAME                     chinese              math         physics            totle                avg
---------- ---------- ---------- ---------- ---------- ----------
chen                              74                  99                 88                261                  87
ck                                 78                  89                 98                265              88.33

使用的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 "

Grade of Students "

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 "

Grade of Students "

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


代表意義 分鐘 小時 日期 月份 週 指令

數字範圍 0-59 0-23 1-31 1-12 0-7 命令或脚本

特殊字符 代表意義

*(星號) 代表任何時刻都接受的意思!舉例來說,範例一內那個日、月、週都是 * , 就代表著『不論何月、何日的禮拜幾的 12:00 都執行後續指令』的意思!

,(逗號) 代表分隔時段的意思。舉例來說,如果要下達的工作是 3:00 與 6:00 時,就會是: 0 3,6 * * * command時間參數還是有五欄,不過第二欄是 3,6 ,代表 3 與 6 都適用!

-(減號) 代表一段時間範圍內,舉例來說, 8 點到 12 點之間的每小時的 20 分都進行一項工作: 20 8-12 * * * command仔細看到第二欄變成 8-12 喔!代表 8,9,10,11,12 都適用的意思!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools