问题
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