bash 保存 Crontab 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6363321/
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-09-18 00:11:33  来源:igfitidea点击:

Saving Crontab files

bashunixcron

提问by Phil Braun

A simple question about Crontab, does it matter where I save crontab files? (creating the time-dependent jobs using crontab -e) or can they be read from any directory?

关于 Crontab 的一个简单问题,我将 crontab 文件保存在哪里有关系吗?(使用 crontab -e 创建依赖于时间的作业)还是可以从任何目录读取它们?

I ask because it seemed that my crontab file got deleted because when I used the crontab -l it didn't return anything. However, I think that is because I saved it as a temporary file: Creating more permanent crontab files

我问是因为我的 crontab 文件似乎被删除了,因为当我使用 crontab -l 时它没有返回任何内容。但是,我认为这是因为我将其保存为临时文件:Creating more Permanent crontab files

回答by Jonathan Leffler

Normally, you use crontabto create the file, and it stores it in the correct place for your machine.

通常,您使用crontab来创建文件,并将其存储在您机器的正确位置。

$ crontab < $HOME/etc/crontab

Then you use crontab -lto list it.

然后你crontab -l用来列出它。

$ crontab -l
#   @(#)$Id: crontab,v 4.2 2007/09/17 02:41:00 jleffler Exp $
#   Crontab file for Home Directory for Jonathan Leffler (JL)
#-----------------------------------------------------------------------------
#Min     Hour    Day     Month   Weekday Command
#-----------------------------------------------------------------------------
0        *       *       *       *       /usr/bin/ksh /work4/jleffler/bin/Cron/hourly
1        1       *       *       *       /usr/bin/ksh /work4/jleffler/bin/Cron/daily
23       1       *       *       1-5     /usr/bin/ksh /work4/jleffler/bin/Cron/weekday
2        3       *       *       0       /usr/bin/ksh /work4/jleffler/bin/Cron/weekly
21       3       1       *       *       /usr/bin/ksh /work4/jleffler/bin/Cron/monthly
$

Where I keep my original is immaterial (but it is under source control in $HOME/etcas it happens). The system has its own copy of the file where it needs it.

我保留原件的位置无关紧要(但它在$HOME/etc发生时受到源代码控制)。系统在需要它的地方有自己的文件副本。

If you try placing the files manually, you will get bitten. It might work, but then again, it might not (and it might change in the future). I wouldn't play the risky game of using other than the kosher command to store crontab files for use by crontab.

如果您尝试手动放置文件,您会被咬到。它可能会起作用,但话又说回来,它可能不会(并且将来可能会发生变化)。除了 kosher 命令之外,我不会玩冒险的游戏来存储 crontab 文件以供crontab.

回答by Charlie Martin

Yes, the cron files mustbe in /var/spool/cron (or some similar directory, depending on system.) See man 8 cron.

是的,cron 文件必须在 /var/spool/cron(或一些类似的目录,取决于系统)中。请参见man 8 cron.

回答by Seth Robertson

If you use the cron api (crontab -eor crontab filenamefor most systems) your cron jobs will be saved in the proper place permanently, whether you log out or not.

如果您使用 cron api(crontab -ecrontab filename对于大多数系统),您的 cron 作业将永久保存在适当的位置,无论您是否注销。

The only way this would not happen is if you were administratively prevented from using cron (not uncommon).

唯一不会发生这种情况的方法是,如果您在管理上被禁止使用 cron(并不少见)。

Remember the format of the file you are creating is in man 5 crontabformat. It is NOT your shell script.

请记住,您正在创建的文件man 5 crontab格式是格式。它不是您的 shell 脚本。