我想从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。
答案 0 :(得分:1)
获得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);
答案 1 :(得分:0)
尝试类似:
# 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')
答案 2 :(得分:0)
了解如何做到这一点。
$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');