简体   繁体   English

我的删除cronjob php函数有什么问题?

[英]What's wrong with my remove cronjob php function?

I'm trying to write a function that helps with deleting cron jobs, I've tried some classes from the net but it didn't work, so I'm writing it myself ,the problem that it's not removing command correctly, it's either deleting it or it's removing half of every cron job that is exist in the file! 我正在尝试编写一个有助于删除cron作业的函数,我已经尝试了一些来自网络的类,但是它不起作用,所以我自己编写了它,这是无法正确删除命令的问题,要么删除它,或者删除文件中存在的每个cron作业的一半!

Here's my function : 这是我的功能:

function delete_cronjob($command)
{
   $outputs = array();
    exec('crontab -l',$outputs);

    for($i=0;$i<count($outputs);$i++)
    {
        if ($outputs[$i]==$command)
            $outputs[$i]=NULL;
    }

      shell_exec('crontab -r');
      for($i=0;$i<count($outputs);$i++)
      {
          if ($outputs[$i]!=NULL)
            shell_exec('(crontab -l ; echo "'.$outputs[$i].'") |uniq - | crontab -');
      }

} }

And this is the kind of cron job I use : 这就是我使用的cron作业:

$command='0 0 * * * ssh -p 22 root@192.168.0.121 "cd /home/sally/;./bckp_rstr.bash _ _ 0 1 test root@192.168.0.196:/home/sally/sal_bckp/ 192.168.0.121 22 1 "';
delete_cronjob($command);

what's wrong ? 怎么了 ?

If it works on your system like it does on mine (Fedora) I would suggest just using file_put_contents, much less to go wrong 如果它在您的系统上像在我的(Fedora)上一样工作,我建议您仅使用file_put_contents,更不要说出错了

Also I store the cron jobs in a database, so I can flag them as active/inactive and just dump out the active ones - along with a field for comments/description of what it does 另外,我还将cron作业存储在数据库中,因此我可以将它们标记为活动/不活动,并仅将活动的作业转储出去-以及用于对其功能进行注释/描述的字段

it has the downside of overwriting any crons edited manually, but I don't tend to do that. 它的缺点是会覆盖任何手动编辑的分支,但是我不倾向于这样做。 The advantage of having a store of crons that can be accessed by al our servers - there is a field that says which server(s) the crons are for - outweighs that. 拥有可以由我们的服务器访问的cron的好处-有一个字段说明cron适用于哪个服务器-胜过这一点。 It also means the the list of crons is backed up along with the rest of the data, and can easily be pushed out to other machines if need be :) worth considering. 这也意味着,将克隆的列表与其余数据一起备份,并且在需要考虑时可以轻松地将其推送到其他计算机:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM