ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Drupal 学习笔记 (5)

Drupal 学习笔记 (5)

原创 Linux操作系统 作者:kirinri 时间:2011-07-01 10:12:01 0 删除 编辑
Ubuntu - Drupal - Job

重启apache2
sudo service apache2 restart

设置定时任务
crontab -e
---每晚20点半执行这个php(项目中这个为业务job)
30 20 * * * /home/user/....../***.php
---每下午16点半执行这个sh(项目中这个为db备份job)
30 16 * * * /home/user/....../***.sh

-设定后要重启定时任务
 /etc/init.d/cron restart

发送mail需要在server安装mail功能,然后定时任务如果失败了可以mail管理员。
附db备份job内容:
#!/bin/sh
name=$(date +%Y-%m-%d)
mysqldump -uroot -p*** --database InvoiceDB > /home/user/.../InvoiceDB$name.sql
tar zcf /home/user/InvoiceDBBackup/SQL$name.tar.gz /home/user/InvoiceDBBackup/InvoiceDB$name.sql
rm /home/hisoft/InvoiceDBBackup/InvoiceDB$name.sql

附业务job内容(一个DBHelp类,操作DB,然后发mail)
#!/usr/bin/php -q
class DBHelper{
//Create Connection
function GetConnection($db_host,$db_username,$db_password){
   $connection=mysql_connect($db_host,$db_username,$db_password);
   //if($connection==false)
   //die("Could not connect to the database:
".mysql_error());
   return $connection;
}
//Select DB
function DBSelect($db_database){
   $db_select=mysql_select_db($db_database);
   //if($db_select==false)
   //die("Could not select the database:
".mysql_error());
   return $db_select;
}
//mysql_query
function Excute($sqlstring){
   $result=mysql_query($sqlstring);
   return $result;
}
//Close Connection
function CloseConnection($connection){
   if($connection!=null)
   mysql_close($connection);
}
}

// Job Function
$db_host='localhost';
$db_database='InvoiceDB';
$db_username='root';
$db_password='Win@2011';
// Db Helper
$dbhelper=new DBHelper();
$connection=$dbhelper->GetConnection($db_host,$db_username,$db_password);
$dbhelper->DBSelect($db_database);
$sqlOverdue = "select ... ";
$sqlOutstanding = "select ... ";
$sqlUpOverdue = "update ... ";
$sqlUpOutstanding = "... ";
$headers = "From: InvoiceAdmin@***-invoice.com\r\n";
// Excute Select SQL
$OverdueResult=$dbhelper->Excute($sqlOverdue);
$OutstandingResult=$dbhelper->Excute($sqlOutstanding);
// Result
while($row=mysql_fetch_array($OverdueResult)){
        $OverdueMailText = "You have an overdue invoice. As follows: \r\n";
    $OverdueMailText = $OverdueMailText . "PO Number: " . $row['field_ponumber_value']."\r\n";
    $OverdueMailText = $OverdueMailText . "Invoice ID: " . $row['field_invoiceid_value']."\r\n";
    $OverdueMailText = $OverdueMailText . "Project ID: " . $row['field_po_project_id_value']."\r\n";
    $OverdueMailText = $OverdueMailText . "Project Name: " . $row['field_po_project_name_value']."\r\n";
    $OverdueMailText = $OverdueMailText . "Invoice Date: " . $row['field_invoicedate_value']."\r\n";
    $OverdueMailText = $OverdueMailText . "PJ Approve Date: " . $row['field_approvedate_value']."\r\n";
    $OverdueMailText = $OverdueMailText . "Due Date: " . $row['field_duedaet_value']."\r\n";
    $curr = "";
    switch($row['field_invoicecurrency_value'])
    {
        case "1":
                $curr = "RMB";
        break;
        case "2":
                $curr = "USD";
        break;
        case "3":
                $curr = "EUR";
        break;
        case "4":
                $curr = "HKD";
        break;
        case "5":
                $curr = "JPY";
        break;
        case "6":
                $curr = "GBR";
        break;
        default:
                $curr = "Currency";
        break;
    }
    $OverdueMailText = $OverdueMailText . "Currency: " . $curr."\r\n";
    $OverdueMailText = $OverdueMailText . "Amount: " . $row['field_invoiceamount_value']."\r\n";
    $OverdueMailText = $OverdueMailText . "Sow Number: " . $row['field_po_sow_number_value']."\r\n";
    $OverdueMailText = $OverdueMailText . "Legal Entity: " . $row['field_po_legal_entity_value']."\r\n";
    mail($row['mailAddress'], 'Overdue Invoice', $OverdueMailText, $headers);
    mail('***@***.com', 'Overdue Invoice mail has been sent to BRM', $OverdueMailText, $headers);
}

while($row=mysql_fetch_array($OutstandingResult)){
        $OutstandingMailText = "You have an outstanding invoice. As follows: \r\n";
        $OutstandingMailText = $OutstandingMailText . "PO Number: " . $row['field_ponumber_value']."\r\n";
    $OutstandingMailText = $OutstandingMailText . "Invoice ID: " . $row['field_invoiceid_value']."\r\n";
    $OutstandingMailText = $OutstandingMailText . "Project ID: " . $row['field_po_project_id_value']."\r\n";
    $OutstandingMailText = $OutstandingMailText . "Project Name: " . $row['field_po_project_name_value']."\r\n";
    $OutstandingMailText = $OutstandingMailText . "Invoice Date: " . $row['field_invoicedate_value']."\r\n";
    $OutstandingMailText = $OutstandingMailText . "PJ Approve Date: " . $row['field_approvedate_value']."\r\n";
    $OutstandingMailText = $OutstandingMailText . "Due Date: " . $row['field_duedaet_value']."\r\n";
    $curr = "";
    switch($row['field_invoicecurrency_value'])
    {
        case "1":
                $curr = "RMB";
        break;
        case "2":
                $curr = "USD";
        break;
        case "3":
                $curr = "EUR";
        break;
        case "4":
                $curr = "HKD";
        break;
        case "5":
                $curr = "JPY";
        break;
        case "6":
                $curr = "GBR";
        break;
        default:
                $curr = "Currency";
        break;
    }
    $OutstandingMailText = $OutstandingMailText . "Currency: " . $curr."\r\n";
    $OutstandingMailText = $OutstandingMailText . "Amount: " . $row['field_invoiceamount_value']."\r\n";
    $OutstandingMailText = $OutstandingMailText . "Sow Number: " . $row['field_po_sow_number_value']."\r\n";
    $OutstandingMailText = $OutstandingMailText . "Legal Entity: " . $row['field_po_legal_entity_value']."\r\n";
    mail($row['mailAddress'], 'Outstanding Invoice', $OutstandingMailText, $headers);
    mail('***@***.com', 'Outstanding Invoice mail has been sent to BRM', $OutstandingMailText, $headers);
}

// Excute Update SQL
$dbhelper->Excute($sqlUpOverdue);
$dbhelper->Excute($sqlUpOutstanding);
// Close Connection
$dbhelper->CloseConnection($connection);

$send = mail('***@***.com', 'Invoice Job Run End', 'End', $headers);

?>
          





来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12476590/viewspace-701125/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-21

  • 博文量
    257
  • 访问量
    498777