无法让 cron.hourly 工作

Fra*_*lea 6 ubuntu cron

我使用 sudo apt-get install cron 安装了 cron,以 root 身份启动它并确认它正在运行

ps -ef
Run Code Online (Sandbox Code Playgroud)

然后我创建了一个包含以下内容的简单脚本:

touch /home/username/cron-test.txt
Run Code Online (Sandbox Code Playgroud)

我使这个脚本文件可执行并将其放入

mv cron-test.sh /etc/cron.hourly
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,它不会被执行,也不会创建文件。我尝试手动运行它并且它有效。

我还尝试了其他 cron 脚本,但它们似乎不起作用。我错过了什么还是我错误地使用了 cron?

我的系统是 Ubuntu 10.10,我的主机已经把它拆了,所以它只安装了几个进程(甚至没有安装 cron)。

Dan*_*ain 10

尝试添加#! /bin/sh为脚本的第一行并删除扩展名,以便名称为/etc/cron.hourly/cron-test

我记得在某处读到 cron 不会运行带有扩展名的文件,因为它在/ect/crontab具有以下内容时使用 runparts :

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Run Code Online (Sandbox Code Playgroud)

以上是我/etc/crontab在Ubuntu 10.04上的内容,安装了cron(我没有编辑过这个文件)

由于/etc/crontab文件使用run-parts,文件名非常严格(感谢 Matteo):

run-parts runs a number of scripts or programs found in a single directory 
directory. Filenames should consist entirely of upper and lower case letters,
digits, underscores, and hyphens. Subdirectories of directory and files with
other names will be silently ignored. Scripts must follow the
#!/bin/interpretername convention in order to be executed. They will not
automatically be executed by /bin/sh. The files found will be run in the
lexical sort order of the filenames.
Run Code Online (Sandbox Code Playgroud)

  • @Matteo:扩展不是 `crontab` 的问题,但是当使用 `/etc/cron.*/` 文件夹时,它们可能会出现问题 (3认同)