默认的 crontab 指令保存在哪里?

默认的 crontab 指令保存在哪里?

当我在还没有任何 cron 的新用户上运行 crontab -l 时,结果如下,命令失败并退出

no crontab for [user]

如果我在新用户上运行 crontab -e,结果如下,并且 crontab 编辑器打开。

no crontab for [user] - using an empty one

下列言辞从何而来?

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

具体哪个文件包含这些指令。

我想运行如下命令:

[instructions file] > temp_file
[a cron job] >> temp_file
crontab temp_file
rm temp_file

但是由于没有新用户的 cron 文件,运行失败:

crontab -l > temp_file
[a cron job] >> temp_file
crontab temp_file
rm temp_file

答案1

该文本实际上似乎是内置于crontab.c源代码中,而不是在执行时从文件中读取的:

        if (add_help_text) {
                fprintf(NewCrontab, 
"# Edit this file to introduce tasks to be run by cron.\n"
"# \n"
"# Each task to run has to be defined through a single line\n"
"# indicating with different fields when the task will be run\n"
"# and what command to run for the task\n"
"# \n"
"# To define the time you can provide concrete values for\n"
"# minute (m), hour (h), day of month (dom), month (mon),\n"
"# and day of week (dow) or use '*' in these fields (for 'any')."
"# \n"
"# Notice that tasks will be started based on the cron's system\n"
"# daemon's notion of time and timezones.\n"
"# \n"
"# Output of the crontab jobs (including errors) is sent through\n"
"# email to the user the crontab file belongs to (unless redirected).\n"
"# \n"
"# For example, you can run a backup of all your user accounts\n"
"# at 5 a.m every week with:\n"
"# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/\n"
"# \n"
"# For more information see the manual pages of crontab(5) and cron(8)\n" 
"# \n"
"# m h  dom mon dow   command\n" );
        }

        /* ignore the top few comments since we probably put them there.
         */

如果无法为用户找到现有的假脱机文件,则该变量add_help_text不为零:crontab

log_it(RealUser, Pid, "BEGIN EDIT", User);
(void) snprintf(n, MAX_FNAME, CRON_TAB(User));
if (!(f = fopen(n, "r"))) {
        if (errno != ENOENT) {
                fprintf(stderr, "%s/: fdopen: %s", n, strerror(errno));
                exit(ERROR_EXIT);
        }
        fprintf(stderr, "no crontab for %s - using an empty one\n",
                User);
        if (!(f = fopen("/dev/null", "r"))) {
                perror("/dev/null");
                exit(ERROR_EXIT);
        }
        add_help_text = 1;
}

答案2

它内置于可执行文件中:

strings $(type -p crontab) | less "+/Edit this file to introduce tasks to be run by cron"

无需咨询来源。我同意《唯一真实文件》中的来源。

相关内容