userName = $userName; } $this->useSudo = $useSudo; } /** * 读取并返回一个定时任务的原数据(命令行) * * @return string $output 任务原数据(命令行) */ public function readCrontab() { $crontabCommandLine = (isset($this->userName) && $this->useSudo) ? sprintf('sudo -n -u %s crontab -l', $this->userName) : ($this->userName ? sprintf('crontab -u %s -l', $this->userName) : 'crontab -l') ; exec($crontabCommandLine . ' 2>&1', $output, $exitCode); /* exec 错误处理 */ if ($exitCode !== 0) { /* 特殊情况 : 正常读取任务的时候,任务是空的,抛出异常的退出代码(exit code)*/ if (!preg_match('/^no crontab for .+$/', $output[0])) { throw new DomainException('Error when trying to read crontab : ' . implode(' ', $output)); } else { $output = ''; } } else { $output = implode("\n", $output); } return $output; } /** * 把任务数据写到任务中 * * @param string $crontabRawData */ public function writeCrontab($crontabRawData) { $crontabRawData = escapeshellarg($crontabRawData); $crontabCommandLine = (isset($this->userName) && $this->useSudo) ? sprintf('echo %s | sudo -n -u %s crontab -', $crontabRawData, $this->userName) : ($this->userName ? sprintf('echo %s | crontab -u %s -', $crontabRawData, $this->userName) : sprintf('echo %s | crontab -', $crontabRawData) ) ; exec($crontabCommandLine . ' 2>&1', $output, $exitCode); /* exec 错误处理 */ if ($exitCode !== 0) { throw new DomainException('Error when trying to write crontab : ' . implode(' ', $output)); } } }