php5.3下使用php管理crontab计划任务
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-27 11:23:09 浏览: 评论:0
php5.3或以上版本可以使用php管理crontab计划任务,下面我先来体验一下,有需要学习了解的朋友可进入参考.
1.使用php-crontab-manager管理计划任务
要求 PHP>=5.3,使用方法举例,代码如下:
- use phpmanagercrontabCrontabManager;
- $crontab = new CrontabManager();
- $crontab->enableOrUpdate('/tmp/my/crontab.txt');
- $crontab->save();
添加一个简单的计划任务,代码如下:
- use phpmanagercrontabCrontabManager;
- $crontab = new Ssh2_crontab_manager();
- $job = $crontab->newJob();
- $job->on('* * * * *');
- $job->onMinute('20-30')->doJob("echo foo");
- $crontab->add($job);
- $job->onMinute('35-40')->doJob("echo bar");
- $crontab->add($job);
- $crontab->save();
php类文件,代码如下:
- <?php
- Class Ssh2_crontab_manager {
- private $connection;
- private $path;
- private $handle;
- private $cron_file;
- function __construct($host=NULL, $port=NULL, $username=NULL, $password=NULL)
- {
- $path_length = strrpos(__FILE__, "/");
- $this->path = substr(__FILE__, 0, $path_length) . '/';
- $this->handle = 'crontab.txt';
- $this->cron_file = "{$this->path}{$this->handle}";
- try
- {
- if (is_null($host) || is_null($port) || is_null($username) || is_null($password)) throw new Exception("The host, port, username and password arguments must be specified!");
- $this->connection = @ssh2_connect($host, $port);
- if ( ! $this->connection) throw new Exception("The SSH2 connection could not be established.");
- $authentication = @ssh2_auth_password($this->connection, $username, $password);
- if ( ! $authentication) throw new Exception("Could not authenticate '{$username}' using pasword: '{$password}'.");
- }
- catch (Exception $e)
- {
- $this->error_message($e->getMessage());
- }
- }
- public function exec()
- {
- $argument_count = func_num_args();
- try
- {
- if ( ! $argument_count) throw new Exception("There is nothing to exececute, no arguments specified.");
- $arguments = func_get_args();
- $command_string = ($argument_count > 1) ? implode(" && ", $arguments) : $arguments[0];
- $stream = @ssh2_exec($this->connection, $command_string);
- if ( ! $stream) throw new Exception("Unable to execute the specified commands: <br />{$command_string}");
- }
- catch (Exception $e)
- {
- $this->error_message($e->getMessage());
- }
- return $this;
- }
- public function write_to_file($path=NULL, $handle=NULL)
- {
- if ( ! $this->crontab_file_exists())
- {
- $this->handle = (is_null($handle)) ? $this->handle : $handle;
- $this->path = (is_null($path)) ? $this->path : $path;
- $this->cron_file = "{$this->path}{$this->handle}";
- $init_cron = "crontab -l > {$this->cron_file} && [ -f {$this->cron_file} ] || > {$this->cron_file}";
- $this->exec($init_cron);
- }
- return $this;
- }
- public function remove_file()
- {
- if ($this->crontab_file_exists()) $this->exec("rm {$this->cron_file}");
- return $this;
- }
- public function append_cronjob($cron_jobs=NULL)
- {
- if (is_null($cron_jobs)) $this->error_message("Nothing to append! Please specify a cron job or an array of cron jobs.");
- $append_cronfile = "echo '";
- $append_cronfile .= (is_array($cron_jobs)) ? implode("n", $cron_jobs) : $cron_jobs;
- $append_cronfile .= "' >> {$this->cron_file}";
- $install_cron = "crontab {$this->cron_file}";
- $this->write_to_file()->exec($append_cronfile, $install_cron)->remove_file();
- return $this;
- }
- public function remove_cronjob($cron_jobs=NULL)
- {
- if (is_null($cron_jobs)) $this->error_message("Nothing to remove! Please specify a cron job or an array of cron jobs.");
- $this->write_to_file();
- $cron_array = file($this->cron_file, FILE_IGNORE_NEW_LINES);
- if (emptyempty($cron_array))
- {
- $this->remove_file()->error_message("Nothing to remove! The cronTab is already empty.");
- }
- $original_count = count($cron_array);
- if (is_array($cron_jobs))
- {
- foreach ($cron_jobs as $cron_regex) $cron_array = preg_grep($cron_regex, $cron_array, PREG_GREP_INVERT);
- }
- else
- {
- $cron_array = preg_grep($cron_jobs, $cron_array, PREG_GREP_INVERT);
- }
- return ($original_count === count($cron_array)) ? $this->remove_file() : $this->remove_crontab()->append_cronjob($cron_array);
- }
- public function remove_crontab()
- {
- $this->remove_file()->exec("crontab -r");
- return $this;
- }
- private function crontab_file_exists()
- {//开源代码phpfensi.com
- return file_exists($this->cron_file);
- }
- private function error_message($error)
- {
- die("<pre style='color:#EE2711'>ERROR: {$error}</pre>");
- }
- }
- ?>
项目地址:https://github.com/MediovskiTechnology/php-crontab-manager
2.Ssh2_crontab_manager 关于php管理计划任务的详细教程
具体内容参考:http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/
参考资料:http://stackoverflow.com/questions/4421020/use-php-to-create-edit-and-delete-crontab-jobs
Tags: php5 3任务 crontab计划任务
- 上一篇:PHP中利用PHPExcel导出Excel示例
- 下一篇:php文件缓存类实例整理
相关文章
- ·用实例分析PHP5异常处理(2013-11-13)
- ·php5的simplexml解析错误(2013-11-13)
- ·php5.3中php-fpm进程管理方式(2014-06-20)
- ·php-screw在php5.4.6中编译失败问题(2014-06-29)
- ·PHP5中哈希创建和验证方法详解(2014-08-22)
- ·PHP5.2.X防止Hash冲突拒绝服务攻击的Patch方法(2014-08-23)
- ·解决php5.3不能连接mssql数据库问题(2014-09-10)
- ·php-fpm参数优化让你的php-fpm(php5.3+)网站跑得更快(2015-09-24)
- ·MAC通过MacPorts配置 PHP54+PHP FPM+NGINX+MYSQL5.5(2015-12-10)
- ·PHP5.5迭代生成器用法实例详解(2019-11-17)
- ·PHP5中实现多态的两种方法实例分享(2020-11-19)
- ·php5.4以下版本json不支持不转义内容中文的解决方法(2021-05-08)
- ·PHP5.5迭代生成器用法实例详解(2021-07-14)
- ·PHP5.2中PDO的简单使用方法(2021-07-21)
- ·php5.5使用PHPMailer-5.2发送邮件的完整步骤(2021-10-31)
- ·php5.x禁用eval的操作方法(2021-10-31)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)