我用sudo apt-get install cron安装了cron,以root身份启动并确认它正在运行
ps -ef
然后我创build了一个简单的脚本,其内容如下:
touch /home/username/cron-test.txt
我制作了这个脚本文件可执行文件并将其放入
mv cron-test.sh /etc/cron.hourly
不过由于某些原因,它不会被执行,文件也不会被创build。 我试着手动运行它,它的工作原理。
我也试过其他的cron脚本,他们似乎不工作。 我错过了什么,或者我不正确地使用cron?
我的系统是Ubuntu 10.10,我的主机已经卸载了,所以只安装了几个进程(甚至不是cron)。
尝试添加#! /bin/sh #! /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 # mh 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 )
以上是Ubuntu 10.04上我/etc/crontab的内容,安装了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.
以下是运行部分手册页的一段引文:
文件名应完全由大写和小写字母,数字,下划线和连字符组成。
事情是,你已经包括一个'。' 在你的文件名(就在'txt'之前)。 那段时间与运行部件用于查找脚本的文件名模式不匹配。
请记住,文件扩展名不是文件系统的一部分! 任何文件扩展名只是文件名的一部分,所以当规则说'文件名',其中包括期限和扩展名。
进一步,
脚本必须遵循#!/ bin / interpretername约定才能执行。
这意味着你必须放在#!/ bin / bash中才能执行你的脚本。