您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

php5.3下使用php管理crontab计划任务

2024/4/24 9:10:36发布4次查看
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类文件,代码如下:
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:
{$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() { //开源代码phprm.com return file_exists($this->cron_file); } private function error_message($error) { die(error: {$error}
); }}?>项目地址: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
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录