使用PHP删除单个cron作业 [英] Deleting individual cron jobs using PHP

查看:110
本文介绍了使用PHP删除单个cron作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php脚本,可在将某些数据插入SQL数据库后设置cron作业:

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');
?>

此cron作业执行另一个脚本,该脚本将SQL DB与mailchimp同步。在此mailchimp php脚本中,它删除了cron选项卡:

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');

一旦发生这种情况,我就会松开cron选项卡中的所有作业(它会删除在之后发送电子邮件的电子邮件每个cron作业),我如何才能做到这一点,所以上一行仅删除 /home/dldl1330/public_html/new/mailchimp.php cron作业?

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?

推荐答案

我使用了上面的建议并提出了解决方案,我不确定它的效率或正确性... 。

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');

?>

这篇关于使用PHP删除单个cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆