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

php5.3或以上版本可以使用php管理crontab计划任务,下面我先来体验一下,有需要学习了解的朋友可进入参考.

1.使用php-crontab-manager管理计划任务

要求 PHP>=5.3,使用方法举例,代码如下:

  1. use phpmanagercrontabCrontabManager;
  2. $crontab = new CrontabManager();
  3. $crontab->enableOrUpdate('/tmp/my/crontab.txt');
  4. $crontab->save();

添加一个简单的计划任务,代码如下:

  1. use phpmanagercrontabCrontabManager;
  2. $crontab = new Ssh2_crontab_manager();
  3. $job = $crontab->newJob();
  4. $job->on('* * * * *');
  5. $job->onMinute('20-30')->doJob("echo foo");
  6. $crontab->add($job);
  7. $job->onMinute('35-40')->doJob("echo bar");
  8. $crontab->add($job);
  9. $crontab->save();

php类文件,代码如下:

  1. <?php
  2. Class Ssh2_crontab_manager {
  3. private $connection;
  4. private $path;
  5. private $handle;
  6. private $cron_file;
  7. function __construct($host=NULL, $port=NULL, $username=NULL, $password=NULL)
  8. {
  9. $path_length = strrpos(__FILE__, "/");
  10. $this->path = substr(__FILE__, 0, $path_length) . '/';
  11. $this->handle = 'crontab.txt';
  12. $this->cron_file = "{$this->path}{$this->handle}";
  13. try
  14. {
  15. 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!");
  16. $this->connection = @ssh2_connect($host, $port);
  17. if ( ! $this->connection) throw new Exception("The SSH2 connection could not be established.");
  18. $authentication = @ssh2_auth_password($this->connection, $username, $password);
  19. if ( ! $authentication) throw new Exception("Could not authenticate '{$username}' using pasword: '{$password}'.");
  20. }
  21. catch (Exception $e)
  22. {
  23. $this->error_message($e->getMessage());
  24. }
  25. }
  26. public function exec()
  27. {
  28. $argument_count = func_num_args();
  29. try
  30. {
  31. if ( ! $argument_count) throw new Exception("There is nothing to exececute, no arguments specified.");
  32. $arguments = func_get_args();
  33. $command_string = ($argument_count > 1) ? implode(" && ", $arguments) : $arguments[0];
  34. $stream = @ssh2_exec($this->connection, $command_string);
  35. if ( ! $stream) throw new Exception("Unable to execute the specified commands: <br />{$command_string}");
  36. }
  37. catch (Exception $e)
  38. {
  39. $this->error_message($e->getMessage());
  40. }
  41. return $this;
  42. }
  43. public function write_to_file($path=NULL, $handle=NULL)
  44. {
  45. if ( ! $this->crontab_file_exists())
  46. {
  47. $this->handle = (is_null($handle)) ? $this->handle : $handle;
  48. $this->path = (is_null($path)) ? $this->path : $path;
  49. $this->cron_file = "{$this->path}{$this->handle}";
  50. $init_cron = "crontab -l > {$this->cron_file} && [ -f {$this->cron_file} ] || > {$this->cron_file}";
  51. $this->exec($init_cron);
  52. }
  53. return $this;
  54. }
  55. public function remove_file()
  56. {
  57. if ($this->crontab_file_exists()) $this->exec("rm {$this->cron_file}");
  58. return $this;
  59. }
  60. public function append_cronjob($cron_jobs=NULL)
  61. {
  62. if (is_null($cron_jobs)) $this->error_message("Nothing to append! Please specify a cron job or an array of cron jobs.");
  63. $append_cronfile = "echo '";
  64. $append_cronfile .= (is_array($cron_jobs)) ? implode("n", $cron_jobs) : $cron_jobs;
  65. $append_cronfile .= "' >> {$this->cron_file}";
  66. $install_cron = "crontab {$this->cron_file}";
  67. $this->write_to_file()->exec($append_cronfile, $install_cron)->remove_file();
  68. return $this;
  69. }
  70. public function remove_cronjob($cron_jobs=NULL)
  71. {
  72. if (is_null($cron_jobs)) $this->error_message("Nothing to remove! Please specify a cron job or an array of cron jobs.");
  73. $this->write_to_file();
  74. $cron_array = file($this->cron_file, FILE_IGNORE_NEW_LINES);
  75. if (emptyempty($cron_array))
  76. {
  77. $this->remove_file()->error_message("Nothing to remove! The cronTab is already empty.");
  78. }
  79. $original_count = count($cron_array);
  80. if (is_array($cron_jobs))
  81. {
  82. foreach ($cron_jobs as $cron_regex) $cron_array = preg_grep($cron_regex, $cron_array, PREG_GREP_INVERT);
  83. }
  84. else
  85. {
  86. $cron_array = preg_grep($cron_jobs, $cron_array, PREG_GREP_INVERT);
  87. }
  88. return ($original_count === count($cron_array)) ? $this->remove_file() : $this->remove_crontab()->append_cronjob($cron_array);
  89. }
  90. public function remove_crontab()
  91. {
  92. $this->remove_file()->exec("crontab -r");
  93. return $this;
  94. }
  95. private function crontab_file_exists()
  96. {//开源代码phpfensi.com
  97. return file_exists($this->cron_file);
  98. }
  99. private function error_message($error)
  100. {
  101. die("<pre >ERROR: {$error}</pre>");
  102. }
  103. }
  104. ?>

项目地址: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