繁体   English   中英

如何使用PHP shell_exec编辑crontab

[英]how to edit crontab with PHP shell_exec

我想从PHP脚本编辑crontab。

$output = shell_exec('crontab -l');

            echo "<pre>";
            print_r($output);
            echo "</pre>";

这被返回。

MAILTO="admin@example.com"
*/2 *   *   *   *   /usr/bin/php5 /var/www/vhosts/example.com/streaming.example.com/index.php admin cron  > /dev/null
MAILTO=""
*/1 *   *   *   *   /opt/psa/admin/sbin/fetch_url 'https://www.example.com/referral/send_referral_email'
MAILTO=""
*/5 *   *   *   *   /opt/psa/admin/sbin/fetch_url ' https://www.example.com/members/send_notif'
MAILTO="admin@example.com"
*/2 *   *   *   *   /usr/bin/php5 /var/www/vhosts/example.com/streaming.example.com/index.php admin cron3 > /dev/null

https://www.example.com/members/send_notif脚本之一应该每五分钟运行一次,但不是。 我看到https前面有一个空格,我认为这可能是原因。 我该如何编辑? 我没有访问cpanel的权限,所以我必须使用PHP。

获得crontab的输出后,请在变量中进行所需的更改,然后将其写入文件:

$output = shell_exec('crontab -l');
$find="' https";
$replace="'https";
$output =str_replace($find, $replace,$output);
$file="/path_to_a_file_which_is_writable/crontab.txt";
file_put_contents($file, $output);

然后将新内容写入crontab:

 shell_exec("crontab ".$file);

尝试类似:

# new crontab EDIT THIS
$crontab = '''MAILTO="admin@example.com"
*/2 *   *   *   *   /usr/bin/php5 /var/www/vhosts/example.com/streaming.example.com/index.php admin cron  > /dev/null
MAILTO=""
*/1 *   *   *   *   /opt/psa/admin/sbin/fetch_url 'https://www.example.com/referral/send_referral_email'
MAILTO=""
*/5 *   *   *   *   /opt/psa/admin/sbin/fetch_url ' https://www.example.com/members/send_notif'
MAILTO="admin@example.com"
*/2 *   *   *   *   /usr/bin/php5 /var/www/vhosts/example.com/streaming.example.com/index.php admin cron3 > /dev/null''';

# echo new cron into cron file
shell_exec('echo "' . $crontab . '" >> mycron')
# install new cron file
shell_exec('crontab mycron')
# delete cron file
shell_exec('rm mycron')

了解如何做。

$url = " https://www.example.com";
$new_url = "https://www.example.com";
$output = shell_exec('crontab -l');
$output = str_replace($url,$new_url,$output);
file_put_contents('/tmp/crontab.txt', $output.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM