Linux 如何以编程方式创建新的 cron 作业?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/610839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:04:31  来源:igfitidea点击:

How can I programmatically create a new cron job?

linuxunixcron

提问by DavidM

I want to be able to programatically add a new cron job, what is the best way to do this?

我希望能够以编程方式添加新的 cron 作业,最好的方法是什么?

From my research, it seems I could dump the current crontab and then append a new one, piping that back into crontab:

我的研究来看,似乎我可以转储当前的 crontab,然后附加一个新的,将其管道回 crontab:

(crontab -l ; echo "0 * * * * wget -O - -q http://www.example.com/cron.php") | crontab -

Is there a better way?

有没有更好的办法?

采纳答案by S.Lott

It's always worked well for me.

它对我来说总是很有效。

You should consider a slightly more sophisticated script that can do three things.

您应该考虑一个稍微复杂一点的脚本,它可以做三件事。

  1. Append a crontab line; assuring that it didn't exist. Adding when it already exists is bad.

  2. Remove the crontab line. Perhaps only warning if it didn't exist.

  3. A combination of the above two features to replace the crontab line.

  1. 附加一个 crontab 行;确保它不存在。在它已经存在时添加是不好的。

  2. 删除 crontab 行。如果它不存在,也许只是警告。

  3. 结合以上两个功能来替换crontab 行。

回答by Alex Fort

You could also edit the cron table text file directly, but your solution seems perfectly acceptable.

您也可以直接编辑 cron 表文本文件,但您的解决方案似乎完全可以接受。

回答by MarkR

The best way if you're running as root, is to drop a file into /etc/cron.d

如果您以 root 身份运行,最好的方法是将文件放入 /etc/cron.d

if you use a package manager to package your software, you can simply lay down files in that directory and they are interpreted as if they were crontabs, but with an extra field for the username, e.g.:

如果您使用包管理器来打包您的软件,您可以简单地在该目录中放置文件,并且它们被解释为好像它们是 crontabs,但有一个额外的用户名字段,例如:

Filename: /etc/cron.d/per_minute

文档名称: /etc/cron.d/per_minute

Content: * * * * * root /bin/sh /home/root/script.sh

内容: * * * * * root /bin/sh /home/root/script.sh

回答by Christian Witts

If you're planning on doing it for a run-once scenario for just wget'ing something, take a look at 'at'

如果您打算为仅执行某些操作的一次性场景执行此操作,请查看“at”

回答by vitaly.v.ch

also you can add your tasks to /etc/cron.*/

您也可以将您的任务添加到 /etc/cron.*/

回答by vitaly.v.ch

man crontab is also useful:

man crontab 也很有用:

CRONTAB(1)

CRONTAB(1)

NAME

姓名

   crontab - manipulate per-user crontabs (Dillon's Cron)

SYNOPSIS

概要

   crontab file [-u user] - replace crontab from file

   crontab - [-u user] - replace crontab from stdin

   crontab -l [user] - list crontab for user

回答by JohnZ

OP's solution has a bug, it might allow entries to be added twice, use below to fix.

OP 的解决方案有一个错误,它可能允许添加两次条目,请使用以下内容进行修复。

(crontab -l ; echo "0 * * * * your_command") | sort - | uniq - | crontab -

回答by Tim McNamara

Assuming that there is already an entry in your crontab, the following command should work relatively well. Note that the $CMDvariable is only there for readability. Sorting before filtering duplicates is important, because uniqonly works on adjacent lines.

假设你的 crontab 中已经有一个条目,下面的命令应该会比较好用。请注意,该$CMD变量仅用于可读性。在过滤重复项之前进行排序很重要,因为uniq仅适用于相邻行。

CMD='wget -O - -q http://www.example.com/cron.php"'
(crontab -l ; echo "0 * * * * $CMD") | sort | uniq | crontab -

If you currently have an empty crontab, you will receive the following error to stderr:

如果您当前有一个空的 crontab,您将收到以下 stderr 错误:

no crontab for user

If you want to avoid this, you can add a little bit of complexity add do something like this:

如果你想避免这种情况,你可以增加一点复杂性,添加做这样的事情:

(crontab -l ; echo "0 * * * * $CMD") 2>&1 | sed "s/no crontab for $(whoami)//"  | sort | uniq | crontab -

回答by Renato Tambellini

Simply change the editor to tee command:

只需将编辑器更改为 tee 命令:

export EDITOR="tee"
echo "0 * * * * /bin/echo 'Hello World'" | crontab -e

回答by RafaSashi

function cronjob_exists($command){

    $cronjob_exists=false;

    exec('crontab -l', $crontab);


    if(isset($crontab)&&is_array($crontab)){

        $crontab = array_flip($crontab);

        if(isset($crontab[$command])){

            $cronjob_exists=true;

        }

    }
    return $cronjob_exists;
}

function append_cronjob($command){

    if(is_string($command)&&!empty($command)&&cronjob_exists($command)===FALSE){

        //add job to crontab
        exec('echo -e "`crontab -l`\n'.$command.'" | crontab -', $output);


    }

    return $output;
}

    append_cronjob('* * * * * curl -s http://localhost/cron/test.php');