Deleting individual cron jobs using PHP

我们两清 提交于 2019-12-06 12:11:41

问题


I have a php script that sets up a cron job once some data is inserted to a SQL database:

<?
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'* * * * * /usr/local/bin/php /home/dldl1330/public_html/new/mailchimp.php'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
?>

This cron job executes another script which syncs the SQL DB with mailchimp. In this mailchimp php script it deletes the cron tab:

echo exec('crontab -r');

Once this happens I loose all jobs in my cron tab (and it removes the email which gets emailed after every cron job), how can I make it so the above line only deletes the /home/dldl1330/public_html/new/mailchimp.php cron job?


回答1:


I used the above advise and came up with a solution, I am not sure how efficient or correct it is.... welcome for comments.

Note: The //Find string section is in there just for my debugging/learning purposes

<?php
//get contents of cron tab
$output = shell_exec('crontab -l');
echo "<pre>$output</pre>";

//Find string
$cronjob = ('* * * * * /usr/local/bin/php /home/dldl1330/public_html/new/mailchimp.php');
if (strstr($output, $cronjob)) {
   echo 'found';
} else {
   echo 'not found';
}

//Copy cron tab and remove string
$newcron = str_replace($cronjob,"",$output);
echo "<pre>$newcron</pre>";

file_put_contents('/tmp/crontab.txt', $newcron.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');

?>


来源:https://stackoverflow.com/questions/15830758/deleting-individual-cron-jobs-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!