您的位置:首页 > 运维架构 > Shell

PHP 、shell、crontab 执行定时任务

2017-06-23 13:53 375 查看
为了实现自动更新网站数据的功能,使用PHP 、shell、crontab 执行定时下载文件、读取文件并更新数据库。

一、思路

1、linux编写shell使用wget下载链接文件。

2、php读取下载的文件并存储数据到数据库。

3、使用linux crontab定时任务执行1、2步骤即可。

代码如下:

1、shell代码

#!/bin/sh
if
#wget 下载文件并把文件按照日期重命名
wget -c http://www.****.cn/***/***C?type=productType -O /home/***/$(date -d "today" +"%Y%m%d")productType.txt -o /***/wget.log
wget -c http://www.****.cn/***/***C?type=product -O /home/***/$(date -d "today" +"%Y%m%d")product.txt -o /***/wget.log
#把下载好的文件复制到PHP项目中
cp /***/$(date -d "today" +"%Y%m%d")productType.txt /***/w***/default/****/upload/$(date -d "today" +"%Y%m%d")productType.txt
cp /***/$(date -d "today" +"%Y%m%d")product.txt /***/upload/$(date -d "today" +"%Y%m%d")product.txt
then
echo "数据下载成功!">>/***/access.log;
date +%Y%m%d%H%I%S>>/***/access.log
else
echo "数据下载失败!">>/***/error.log;
date +%Y%m%d%H%I%S>>/***/error.log
fi


2、PHP代码

<?php
/**
* Created by PhpStorm.
* User: liang
* Date: 2017/6/19
* Time: 15:59
*/

$handler = mysql_connect("localhost", "root", "****");
mysql_select_db("****_products");
mysql_query("SET AUTOCOMMIT=0");//设置为不自动提交,因为MYSQL默认立即执行
mysql_query("BEGIN");//开始事务
if (empty($dopost)) $dopost = "";
$file = "upload/".date("Ymd",time()).'productType.txt';
$get = fopen($file, 'r');//mysql读取文件
if ($get) {
$del = "DELETE FROM `#@__products_type`";
$js = $dsql->ExecuteNoneQuery($del);
while (!feof($get)) {
$oneline = fgets($get);
$contents = explode("ξδθ", $oneline);
$id = $contents[0];
$name = $contents[1];
$er = $contents[2];
$err = $contents[3];
if ($name == null || $err != null) {
echo "本条数据格式不正确,上传失败" . '<br/>';
} else {
echo $id . "|" . $name . '<br/>';
$inquery = "INSERT INTO `#@__products_type`(id,name) VALUES ('$id','$name')";
$dsql->ExecuteNoneQuery($inquery);
}
}
} else {
echo "更新产品类型失败!请使用“上传产品资料”功能更新数据!" . '</br>';
die;
}
$file = "upload/".date("Ymd",time()).'product.txt';
$fh = fopen($file, 'r');
if ($fh) {
echo "内容为:" . '<br/>';
while (!feof($fh)) {
$oneline = fgets($fh);
$contents = explode("ξδθ", $oneline);
$id = $contents[0];
$progress = $contents[1];
$testDepartmentID = $contents[2];
$ProductName = $contents[3];
$ProductModel = $contents[4];
$ProductType = $contents[5];
$ProductT
4000
ypeName = $contents[6];
$SendCompany = $contents[7];
$SendCompanyAddress = $contents[8];
$SendCompanyZIP = $contents[9];
$SendCompanyPhone = $contents[10];
$SendCompanyFax = $contents[11];
$SendCompanyAtificialPerson = $contents[12];
$ReportNo = $contents[13];
$ReportResult = $contents[14];
$XKZNo = $contents[15];
$XKZValidDate_Begin = $contents[16];
$XKZValidDate_End = $contents[17];
$deleted = $contents[18];
$er = $contents[19];
$err = $contents[20];

if ($deleted == null || $err != null) {
echo "本条数据格式不正确,上传失败" . '<br/>';
} else {

$row = $dsql->GetOne("SELECT id FROM `#@__products` WHERE ReportNo = '$ReportNo'");
$shuzu = $row['id'];
echo $id . "|" . $progress . "|" . $testDepartmentID . "|" . $ProductName . "|" . $ProductModel . "|" . $ProductType . "|" . $ProductTypeName . "|" . $SendCompany . "|" . $SendCompanyAddress . "|" . $SendCompanyZIP . "|" . $SendCompanyPhone . "|" . $SendCompanyFax . "|" . $SendCompanyAtificialPerson . "|" . $ReportNo . "|" . $ReportResult . "|" . $XKZNo . "|" . $XKZValidDate_Begin . "|" . $XKZValidDate_End . "|" . $deleted . '<br/>';
if (!empty($shuzu)) {
$update = "update `#@__products` SET id='$id',progress='$progress',
testDepartmentID='$testDepartmentID',
ProductTypeName = '$ProductTypeName',
ProductName = '$ProductName',
ProductModel = '$ProductModel',
ProductType = '$ProductType',
SendCompany='$SendCompany',
SendCompanyAddress = '$SendCompanyAddress',
SendCompanyZIP='$SendCompanyZIP',
SendCompanyPhone = '$SendCompanyPhone',
SendCompanyFax='$SendCompanyFax',
SendCompanyAtificialPerson = '$SendCompanyAtificialPerson',
ReportNo='$ReportNo',
ReportResult = '$ReportResult',
XKZNo='$XKZNo',
XKZValidDate_Begin = '$XKZValidDate_Begin',
XKZValidDate_End='$XKZValidDate_End',
deleted = '$deleted'
WHERE ReportNo = '$ReportNo' ";
$dsql->ExecuteNoneQuery($update);
} else {
$inquery = "INSERT INTO `#@__products`(id,progress,testDepartmentID,ProductName,ProductModel,ProductType,ProductTypeName,SendCompany,SendCompanyAddress,SendCompanyZIP,SendCompanyPhone,SendCompanyFax,SendCompanyAtificialPerson,ReportNo,ReportResult,XKZNo,XKZValidDate_Begin,XKZValidDate_End,deleted)
VALUES ('$id','$progress','$testDepartmentID','$ProductName','$ProductModel','$ProductType','$ProductTypeName','$SendCompany','$SendCompanyAddress','$SendCompanyZIP','$SendCompanyPhone','$SendCompanyFax','$SendCompanyAtificialPerson','$ReportNo','$ReportResult','$XKZNo','$XKZValidDate_Begin','$XKZValidDate_End','$deleted')";
$dsql->ExecuteNoneQuery($inquery);
}
}
}
mysql_query('COMMIT') or die(mysql_error());//执行事务
} else {
echo "更新产品数据失败!请使用“上传产品资料”功能更新数据!";
mysql_query('ROLLBACK') or die(mysql_error());//回滚事务
}
fclose($get);
fclose($fh);


3、定时任务

每周五上午10点更新数据  *(分)  *(时)  *(日)  *(月)  *(周)
[root@localhost]# crontab  -l
00 10 * * 5 /home/****/***g.sh
05 10 * * 5 /usr/bin/curl http://www.****.net/****/***.php


经过测试更新成功!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php shell 存储 数据库