Я пытаюсь добавить задание cron, используя следующий код. Но проблема не в том, что я получаю соответствующий результат, ни в какой ошибке. Когда я проверяю crons с помощью шпатлевки, cron не добавляется. Для подключения требуется расширение php_ssh2.
<?php
Class Test {
// Constructor to make connection
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("Please specify the host, port, username and password!");
if (!function_exists("ssh2_connect"))
die('Function ssh2_connect not found, you cannot use ssh2 here');
$this->connection = @ssh2_connect($host, $port);
//var_dump($this->connection); die('heref');
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 password: '{$password}'.");
}
catch (Exception $e)
{
echo $e->getMessage();
die;
}
}
// To check if the crontab.txt file exists
private function crontab_file_exists() {
return file_exists($this->cron_file);
}
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)) {
echo "Nothing to append! Please specify a cron job or an array of cron jobs.";
die;
}
//echo $this->cron_file; die;
$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);
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}";
echo $init_cron = "crontab -l > {$this->cron_file} && [ -f {$this->cron_file} ] || > {$this->cron_file}";
die;
$this->exec($init_cron);
}
return $this;
}
public function exec() {
$argument_count = func_num_args();
try
{
if ( ! $argument_count) throw new Exception("There is nothing to execute, 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)
{
echo $e->getMessage();
die;
}
return $this;
}
}
$Runobj = new Test('12.0.8.158', 22, 'test', 'mind@123');
$response = $Runobj->append_cronjob('* * * * * command >/dev/null');
echo "<pre>"; print_r($response);
?>
На самом деле это был учебник, который я выбрал. Пожалуйста помоги! Заранее спасибо.