PHP 之 定时任务

定时任务命令

1.定时任务服务提供crontab命令来设定服务
2.crontab -e  //编辑某个用户的cron的服务
3.crontab -l  //列出某个用户cron服务的详细内容
4.crontab -r  //删除某个用户的cron服务

这里写图片描述

这里写图片描述

定时任务结合PHP的案列

## php 代码 db.php
<?php

class Db {
    static private $_instance;
    static private $_connectSource;
    private $_dbConfig = array(
        'host' => '127.0.0.1',
        'user' => 'root',
        'password' => '',
        'database' => 'video',
    );

    private function __construct() {
    }

    static public function getInstance() {
        if(!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function connect() {
        if(!self::$_connectSource) {
            self::$_connectSource = @mysql_connect($this->_dbConfig['host'], $this->_dbConfig['user'], $this->_dbConfig['password']);  

            if(!self::$_connectSource) {
                throw new Exception('mysql connect error ' . mysql_error());
                //die('mysql connect error' . mysql_error());
            }

            mysql_select_db($this->_dbConfig['database'], self::$_connectSource);
            mysql_query("set names UTF8", self::$_connectSource);
        }
        return self::$_connectSource;
    }
}
/*$connect = Db::getInstance()->connect();

$sql = "select * from video";
$result = mysql_query($sql, $connect);
echo mysql_num_rows($result);
var_dump($result);*/
## file.php
<?php

class File {
    private $_dir;

    const EXT = '.txt';

    public function __construct() {
        $this->_dir = dirname(__FILE__) . '/files/';
    }
    public function cacheData($key, $value = '', $cacheTime = 0) {
        $filename = $this->_dir  . $key . self::EXT;

        if($value !== '') { // 将value值写入缓存
            if(is_null($value)) {
                return @unlink($filename);
            }
            $dir = dirname($filename);
            if(!is_dir($dir)) {
                mkdir($dir, 0777);
            }

            $cacheTime = sprintf('%011d', $cacheTime);
            return file_put_contents($filename,$cacheTime . json_encode($value));
        }

        if(!is_file($filename)) {
            return FALSE;
        } 
        $contents = file_get_contents($filename);
        $cacheTime = (int)substr($contents, 0 ,11);
        $value = substr($contents, 11);
        if($cacheTime !=0 && ($cacheTime + filemtime($filename) < time())) {
            unlink($filename);
            return FALSE;
        }
        return json_decode($value, true);

    }
}

$file = new File();

echo $file->cacheData('test1');
## cron.php
<?php

// 让crontab定时执行的脚本程序     */5 * * * * /usr/bin/php /data/www/app/cron.php

// 想获取video中 6条数据

require_once('./db.php');
require_once('./file.php');

$sql = "select * from video where status = 1 order by orderby desc";
try {
    $connect = Db::getInstance()->connect();
} catch(Exception $e) {
    // $e->getMessage();
    file_put_contents('./logs/'.date('y-m-d') . '.txt' , $e->getMessage());
    return;
}
$result = mysql_query($sql, $connect); 
$videos = array();
while($video = mysql_fetch_assoc($result)) {
    $videos[] = $video;
}
$file = new File();
if($videos) {
    $file->cacheData('index_cron_cahce', $videos);
} else {
    file_put_contents('./logs/'.date('y-m-d') . '.txt' , "没有相关数据");
}
return;

最后创建定时任务 调用cron.php,调用列子的时候,建议大家,把列子中的数据库和表的名字改下,以免出错!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流年师兄要努力りゅう

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值