这是我第一次通过其 /etc/cron.hourly 目录使用 cron 每小时运行一次脚本。
由于某种原因,该脚本似乎无法运行。
这就是我所做的:
在 /etc/cron.hour 中添加了基本测试脚本:
/etc/cron.hourly# ll
-rwxr-xr-x 1 root root 152 Jun 20 23:26 test.bash*
这是脚本:
/etc/cron.hourly# cat test.bash
#!/bin/bash
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
echo "This line from test.bash" > /tmp/from.test.script
这是/etc/crontab:
/etc/cron.hourly# cat /etc/crontab
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
Cron 包是默认安装的,所以接下来,我运行/etc/init.d/cron restart
,并等到 :17。 /tmp 中仍然没有输出痕迹。
知道我还能尝试什么吗?
/etc/cron.hourly# run-parts --list /etc/cron.hourly
/etc/cron.hourly# run-parts --test /etc/cron.hourly
/etc/cron.hourly# strace run-parts /etc/cron.hourly
execve("/bin/run-parts", ["run-parts", "/etc/cron.hourly"], [/* 16 vars */]) = 0
brk(0) = 0x7f8000
uname({sys="Linux", node="sheevaplug", ...}) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6efe000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=17282, ...}) = 0
mmap2(NULL, 17282, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb6ef9000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/arm-linux-gnueabi/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0XX\1\0004\0\0\0"..., 512) = 512
lseek(3, 1231644, SEEK_SET) = 1231644
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 1400) = 1400
lseek(3, 1231204, SEEK_SET) = 1231204
read(3, "A'\0\0\0aeabi\0\1\35\0\0\0\0054T\0\6\2\10\1\t\1\22\4\24\1\25\1"..., 40) = 40
fstat64(3, {st_mode=S_IFREG|0755, st_size=1233044, ...}) = 0
mmap2(NULL, 1275160, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6da3000
mprotect(0xb6ecd000, 32768, PROT_NONE) = 0
mmap2(0xb6ed5000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x12a) = 0xb6ed5000
mmap2(0xb6ed8000, 9496, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb6ed8000
close(3) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6ef8000
set_tls(0xb6ef86d0, 0xb6ef8da7, 0xb6ef8da8, 0xb6ef86d0, 0xb6f00000) = 0
mprotect(0xb6ed5000, 8192, PROT_READ) = 0
mprotect(0x12000, 4096, PROT_READ) = 0
mprotect(0xb6eff000, 4096, PROT_READ) = 0
munmap(0xb6ef9000, 17282) = 0
umask(022) = 022
brk(0) = 0x7f8000
brk(0x819000) = 0x819000
rt_sigaction(SIGCHLD, {0x931c, [], SA_NOCLDSTOP|0x4000000}, NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], NULL, 8) = 0
open("/etc/cron.hourly", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
fcntl64(3, F_GETFD) = 0x1 (flags FD_CLOEXEC)
getdents(3, /* 6 entries */, 32768) = 128
getdents(3, /* 0 entries */, 32768) = 0
close(3) = 0
exit_group(0) = ?
答案1
您使用的是哪个发行版?根据Ubuntu 的运行部件手册:
如果未给出 --lsbsysinit 选项和 --regex 选项,则名称必须完全由 ASCII 大写和小写字母、ASCII 数字、ASCII 下划线和 ASCII 减号连字符组成。
因此run-parts
不会运行名为 的脚本test.bash
。借助此规则,您可以避免意外运行重命名的脚本,例如*.old
或*.dpkg-dist
*.dpkg-old
通过 dpkg 升级。