簡體   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