Linux 如何通过脚本创建 crontab
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4880290/
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
How do I create a crontab through a script
提问by gaurav
I need to add a cron job thru a script I run to set up a server. I am currently using Ubuntu. I can use crontab -e
but that will open an editor to edit the current crontab. I want to do this programmatically.
我需要通过我运行的脚本添加一个 cron 作业来设置服务器。我目前正在使用 Ubuntu。我可以使用,crontab -e
但这会打开一个编辑器来编辑当前的 crontab。我想以编程方式执行此操作。
Is it possible to do so?
有可能这样做吗?
采纳答案by Jason Stelzer
Cron jobs usually are stored in a per-user file under /var/spool/cron
Cron 作业通常存储在每个用户的文件中 /var/spool/cron
The simplest thing for you to do is probably just create a text file with the job configured, then copy it to the cron spool folder and make sure it has the right permissions (600).
您要做的最简单的事情可能是创建一个配置了作业的文本文件,然后将其复制到 cron spool 文件夹并确保它具有正确的权限 (600)。
回答by Dirk Eddelbuettel
Well /etc/crontab
just an ascii file so the simplest is to just
那么/etc/crontab
只是一个 ascii 文件,所以最简单的就是
echo "*/15 * * * * root date" >> /etc/crontab
which will add a job which will email you every 15 mins. Adjust to taste, and test via grep
or other means whether the line was already added to make your script idempotent.
这将添加一个工作,该工作将每 15 分钟向您发送电子邮件。根据口味进行调整,并通过grep
或其他方式测试是否已添加该行以使您的脚本具有幂等性。
On Ubuntu et al, you can also drop files in /etc/cron.*
which is easier to do and test for---plus you don't mess with (system) config files such as /etc/crontab
.
在 Ubuntu 等上,您还可以删除/etc/cron.*
更容易执行和测试的文件---此外,您不会弄乱(系统)配置文件,例如/etc/crontab
.
回答by IvanGoneKrazy
In Ubuntu and many other distros, you can just put a file into the /etc/cron.d
directory containing a single line with a valid crontabentry. No need to add a line to an existing file.
在 Ubuntu 和许多其他发行版中,您只需将一个文件放入/etc/cron.d
包含一行有效crontab条目的目录中。无需在现有文件中添加一行。
If you just need something to run daily, just put a file into /etc/cron.daily
. Likewise, you can also drop files into /etc/cron.hourly
, /etc/cron.monthly
, and /etc/cron.weekly
.
如果您只需要每天运行一些东西,只需将文件放入/etc/cron.daily
. 同样的,你也可以文件拖放到/etc/cron.hourly
,/etc/cron.monthly
和/etc/cron.weekly
。
回答by cledoux
Crontab files are simply text files and as such can be treated like any other text file. The purpose of the crontab
command is to make editing crontab files safer. When edited through this command, the file is checked for errors and only saved if there are none.
Crontab 文件只是文本文件,因此可以像对待任何其他文本文件一样对待。该crontab
命令的目的是使编辑 crontab 文件更安全。通过此命令编辑时,将检查文件是否有错误,只有在没有错误时才保存。
crontab [path to file]
can be used to specify a crontab stored in a file. Like crontab -e
, this will only install the file if it is error free.
crontab [path to file]
可用于指定存储在文件中的 crontab。就像crontab -e
,这只会在文件没有错误的情况下安装。
Therefore, a script can either directly write cron tab files, or write them to a temporary file and load them with the crontab [path to temp file]
command. Writing directly saves having to write a temporary file, but it also avoids the safety check.
因此,脚本可以直接写入 cron 选项卡文件,也可以将它们写入临时文件并使用crontab [path to temp file]
命令加载它们。直接写可以省去写临时文件的麻烦,但也避免了安全检查。
回答by Paused until further notice.
For user crontabs (including root), you can do something like:
对于用户 crontabs(包括 root),您可以执行以下操作:
crontab -l -u user | cat - filename | crontab -u user -
where the file named "filename" contains items to append. You could also do text manipulation using sed
or another tool in place of cat
. You should use the crontab
command instead of directly modifying the file.
其中名为“filename”的文件包含要追加的项目。您还可以使用sed
或其他工具代替cat
. 您应该使用crontab
命令而不是直接修改文件。
A similar operation would be:
类似的操作是:
{ crontab -l -u user; echo 'crontab spec'; } | crontab -u user -
If you are modifying or creating system crontabs, those may be manipulated as you would ordinary text files. They are stored in the /etc/cron.d
, /etc/cron.hourly
, /etc/cron.daily
, /etc/cron.weekly
, /etc/cron.monthly
directories and in the files /etc/crontab
and /etc/anacrontab
.
如果您正在修改或创建系统 crontab,则可以像处理普通文本文件一样操作它们。它们存储在/etc/cron.d
, /etc/cron.hourly
, /etc/cron.daily
, /etc/cron.weekly
,/etc/cron.monthly
目录以及文件/etc/crontab
和/etc/anacrontab
.
回答by Joe Casadonte
Here's a one-liner that doesn't use/require the new job to be in a file:
这是一个单行,不使用/要求将新作业放在文件中:
(crontab -l 2>/dev/null; echo "*/5 * * * * /path/to/job -with args") | crontab -
The 2>/dev/null
is important so that you don't get the no crontab for username
message that some *nixes produce if there are currently no crontab entries.
这2>/dev/null
很重要,这样no crontab for username
如果当前没有 crontab 条目,您就不会收到某些 *nixes 产生的消息。
回答by user2845840
As a correction to those suggesting crontab -l | crontab -
: This does not work on every system. For example, I had to add a job to the root crontab on dozens of servers running an old version SUSE (don't ask why). Old SUSEs prepend comment lines to the output of crontab -l
, making crontab -l | crontab -
non-idempotent (Debian recognizes this problem in the crontab manpage and patched its version of Vixie Cron to change the default behaviour of crontab -l
).
作为对那些建议的更正crontab -l | crontab -
:这不适用于每个系统。例如,我不得不在运行旧版本 SUSE 的数十台服务器上的 root crontab 中添加一个作业(不要问为什么)。旧的 SUSE 在 的输出前添加注释行crontab -l
,使crontab -l | crontab -
非幂等性(Debian 在 crontab 联机帮助页中识别出这个问题并修补其 Vixie Cron 版本以更改 的默认行为crontab -l
)。
To edit crontabs programmatically on systems where crontab -l
adds comments, you can try the following:
要在crontab -l
添加注释的系统上以编程方式编辑 crontab ,您可以尝试以下操作:
EDITOR=cat crontab -e > old_crontab; cat old_crontab new_job | crontab -
EDITOR=cat crontab -e > old_crontab; cat old_crontab new_job | crontab -
EDITOR=cat
tells crontab to use cat
as an editor (not the usual default vi), which doesn't change the file, but instead copies it to stdout. This might still fail if crontab -
expects input in a format different from what crontab -e
outputs. Do not try to replace the final crontab -
with crontab -e
- it will not work.
EDITOR=cat
告诉 crontabcat
用作编辑器(不是通常的默认 vi),它不会更改文件,而是将其复制到标准输出。如果crontab -
期望输入的格式与crontab -e
输出的格式不同,这可能仍然会失败。不要尝试更换最后crontab -
用crontab -e
-这是行不通的。
回答by ganesh pathak
Even more simple answer to you question would be:
对你的问题更简单的答案是:
echo "0 1 * * * /root/test.sh" | tee -a /var/spool/cron/root
You can setup cronjobs on remote servers as below:
您可以在远程服务器上设置 cronjobs,如下所示:
#!/bin/bash
servers="srv1 srv2 srv3 srv4 srv5"
for i in $servers
do
echo "0 1 * * * /root/test.sh" | ssh $i " tee -a /var/spool/cron/root"
done
In Linux, the default location of the crontab
file is /var/spool/cron/
. Here you can find the crontab
files of all users. You just need to append your cronjob entry to the respective user's file. In the above example, the root user's crontab file is getting appended with a cronjob to run /root/test.sh
every day at 1 AM.
在 Linux 中,crontab
文件的默认位置是/var/spool/cron/
. 在这里您可以找到crontab
所有用户的文件。您只需要将您的 cronjob 条目附加到相应用户的文件中。在上面的例子中,root 用户的 crontab 文件附加了一个 cronjob,/root/test.sh
每天凌晨 1 点运行。
回答by monklof
I have written a crontab deploy tool in python: https://github.com/monklof/deploycron
我用python写了一个crontab部署工具:https: //github.com/monklof/deploycron
pip install deploycron
Install your crontab is very easy, this will merge the crontab into the system's existing crontab.
安装你的 crontab 非常简单,这会将 crontab 合并到系统现有的 crontab 中。
from deploycron import deploycron
deploycron(content="* * * * * echo hello > /tmp/hello")
回答by Brian Smith
Here is how to modify cron a entry without directly editing the cron file (which is frowned upon).
以下是如何在不直接编辑 cron 文件的情况下修改 cron 条目(这是不受欢迎的)。
crontab -l -u <user> | sed 's/find/replace/g' | crontab -u <user> -
If you want to remove a cron entry, use this:
如果要删除 cron 条目,请使用以下命令:
crontab -l -u <user> | sed '/find/d' | crontab -u <user> -
I realize this is not what gaurav was asking for, but why not have all the solutions in one place?
我意识到这不是 gaurav 所要求的,但为什么不将所有解决方案集中在一个地方?