Crontab 不执行 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16218705/
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
Crontab not executing bash script
提问by Barry Jarvis
I very very rarely use Linux and so don't have any experience with bash scripts and cron jobs. This is in fact my first attempt. So it's probably something really simple to fix.
我很少使用 Linux,因此对 bash 脚本和 cron 作业没有任何经验。这实际上是我的第一次尝试。所以这可能很容易修复。
I have the following:
我有以下几点:
/etc/cron.d/clear-mixtape-dir.sh permissions are: 644
/etc/cron.d/clear-mixtape-dir.sh 权限为:644
#!/bin/bash
# Clears the /tmp/mixtape2 directory
rm -rf "/tmp/mixtape2/"*
My crontab file looks like so:
我的 crontab 文件如下所示:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
*/15 * * * * /etc/cron.d/clear-mixtape-dir.sh >/dev/null 2>&1
I'm trying to execute the .sh script every 15 minutes.
我试图每 15 分钟执行一次 .sh 脚本。
Everything i've found says this should work, but it doesn't.
我发现的一切都表明这应该有效,但事实并非如此。
Does anything like file permissions (on files within /tmp/mixtape2/) matter in this case? Or perhaps the permissions set on the actual .sh script - maybe they need setting to executable?
在这种情况下,文件权限(/tmp/mixtape2/ 中的文件)之类的东西重要吗?或者可能是在实际 .sh 脚本上设置的权限 - 也许他们需要设置为可执行文件?
Any advice appreciated.
任何建议表示赞赏。
回答by octopusgrabbus
Note: These comments refer to /etc/crontab.
注意:这些注释是指 /etc/crontab。
Before doing anything else, which cron are you accessing crontab -e
or
在做任何其他事情之前,您正在访问哪个 croncrontab -e
或
su -vim
<your-favorite-editor> /etc/crontab
If you are using crontab -e, then no user field exists in that form of crontab. That might be why you're not running.
如果您使用的是 crontab -e,则该形式的 crontab 中不存在用户字段。这可能就是你不跑步的原因。
In your example, your user field is *. I would make it root or a user that has proper permissions.
在您的示例中,您的用户字段是 *. 我会将其设为 root 或具有适当权限的用户。
Before running this program, I would make a dummy crontab entry that just does echo "Hello" and runs every minute. Get that to work on which ever crontab you're editing (crontab -e or vim /etc/crontab). Then using that as a template, get your script to run.
在运行这个程序之前,我会创建一个虚拟的 crontab 条目,它只回显“Hello”并每分钟运行一次。让它在您正在编辑的任何 crontab 上工作(crontab -e 或 vim /etc/crontab)。然后使用它作为模板,让你的脚本运行。
Next, see if cron is running:
接下来,查看 cron 是否正在运行:
ps -ef | grep cron
ps -ef | grep cron
If it is not running, become root and start it by enter
如果它没有运行,请成为 root 并通过输入启动它
/etc/init.d/cron start
(Ubuntu and Red Hat).
/etc/init.d/cron start
(Ubuntu 和红帽)。
You already have a good answer suggesting you add root as the user because of a permissions problem. I'm going to suggest more things to help you debug. I have run into a lot of cron problems over the years.
您已经有一个很好的答案,建议您由于权限问题将 root 添加为用户。我会建议更多的东西来帮助你调试。这些年来,我遇到了很多 cron 问题。
1) Set the email to a known address, unless you will continually monitor root's email
1) 将电子邮件设置为已知地址,除非您将持续监视 root 的电子邮件
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/local/bin:/usr/bin
[email protected]
HOME=/
2) Until everything runs properly, take out the >/dev/null 2>&1
out of your cron entry, so you see the outputs in your email generated after the script runs.
2) 在一切正常运行之前,取出>/dev/null 2>&1
您的 cron 条目,以便您看到脚本运行后生成的电子邮件中的输出。
3) Bump */15
down to an interval greater than it takes your script to run -- likr */5
, so the script runs more often.
3)*/15
降低到大于脚本运行所需的时间间隔 -- likr */5
,因此脚本运行得更频繁。
4) I do not know the exact reason, but scripts I run out of cron have to set up their own environments despite being run as that user in cron. This may include steps like cd /home/script-owner
and running source .bashrc
and calling other script(s) that set environment variables.
4)我不知道确切的原因,但是尽管在 cron 中以该用户身份运行,但我用完 cron 的脚本必须设置自己的环境。这可能包括诸如cd /home/script-owner
运行source .bashrc
和调用其他设置环境变量的脚本之类的步骤。
回答by Chris Burgess
Remove the .sh extension from the script in /etc/cron.d
and it will be called.
从脚本中删除 .sh 扩展名,/etc/cron.d
它将被调用。
run-parts
ignores files with a period in the name, so the .sh extension is preventing your script from running.
run-parts
忽略名称中带有句点的文件,因此 .sh 扩展名会阻止您的脚本运行。
From man cron
-
来自man cron
-
Files must conform to the same naming convention as used by run-parts(8): they must consist solely of upper- and lower-case letters, digits, underscores, and hyphens.
文件必须符合 run-parts(8) 使用的相同命名约定:它们必须仅由大写和小写字母、数字、下划线和连字符组成。
回答by iamauser
*/15 * * * * root /etc/cron.d/clear-mixtape-dir.sh >/dev/null 2>&1
Add user root
because your permission seems to be only for root.
添加用户,root
因为您的权限似乎仅适用于 root。