Cron 作业未启动
我有一个 cron 作业,我想每 5 分钟执行一次:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /scr_temp/scheduleSpider.sh
在 /var/spool/cron/crontabs/root
中
cron 应该执行一个 shell 脚本:
#!/bin/sh
if [ ! -f "sync.txt" ]; then
touch "sync.txt"
chmod 777 /scr_temp
curl someLink
fi
在命令行中工作正常,但在 cron 中则不行。然而,cron 本身已启动,但脚本并未启动。
我读到了路径问题,但我并不真正理解它。我设置了一个 cron 将一些环境数据写入文件。这是输出:
HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
SHELL=/bin/sh
如果我在命令行中执行 env 命令,我会得到以下 PATH 输出
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
我必须在 shell 脚本中设置什么路径?
I have a cron job that I want to execute every 5 minutes:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /scr_temp/scheduleSpider.sh
In /var/spool/cron/crontabs/root
The cron should execute a shell script:
#!/bin/sh
if [ ! -f "sync.txt" ]; then
touch "sync.txt"
chmod 777 /scr_temp
curl someLink
fi
That works fine from command line but not from cron. However the cron itself is startet but the script does not start.
I read about the path problem but I dont really understand it. I setup a cron that writes some env data to a file. This is the output:
HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
SHELL=/bin/sh
If I execute the env command in command line I get following output for PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
What path do I have to set in my shell script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你的
$PATH
没问题;别管它了。在 Ubuntu 上,您调用的所有命令(touch
、chmod
、curl
)都位于/bin
中/或/usr/bin
。你是如何设置 cron 作业的?您是否以root身份运行
crontab some-file
?似乎
/etc/crontab
是作为 root 运行 cron 命令的常用机制。在我的 Ubuntu 系统上,sudo crontab -l
显示no crontab for root
。像任何非 root 帐户一样以 root 身份运行 crontab 应该没问题,但您可以考虑使用 /etc/crontab 来代替。请注意,它使用与普通 crontab 不同的语法,如/etc/crontab
顶部的注释中所述:运行
sudo crontab -l
。它显示你的命令吗?暂时修改您的脚本,使其始终产生一些可见的输出。例如,在
#!/bin/sh
之后添加以下内容:几分钟后查看
/tmp/scheduleSpider.sh.log
中的内容。 (您可以将命令设置为每分钟运行一次,这样您就不必等待很长时间才能得到结果。)如果有效(应该如此),您可以在脚本中添加更多echo
命令来查看详细说明它在做什么。看起来您的脚本被设计为只运行一次;它会创建
sync.txt
文件以防止其再次运行。这可能是你问题的根源(咳咳)。你的意图是什么?您是否想在运行命令后删除sync.txt
,但只是忘了这样做?root
在 Ubuntu 上的主目录是/root
。脚本第一次运行时,它应该创建/root/sync.txt
。该文件存在吗?如果是的话,现在多大了?请注意,
curl someLink
(假设someLink
是有效的 URL)只会将指定链接的内容转储到标准输出。这是您的意图吗(它将显示为发送给root
的电子邮件?或者您只是没有向我们展示整个命令?Your
$PATH
is fine; leave it alone. On Ubuntu, all the commands you're invoking (touch
,chmod
,curl
) are in/bin
and/or/usr/bin
.How did you set up the cron job? Did you run
crontab some-file
as root?It seems that
/etc/crontab
is the usual mechanism for running cron commands as root. On my Ubuntu system,sudo crontab -l
saysno crontab for root
. Runningcrontab
as root, as you would for any non-root account, should be ok, but you might consider using/etc/crontab
instead. Note that it uses a different syntax than an ordinary crontab, as explained in the comments at the top of/etc/crontab
:Run
sudo crontab -l
. Does it show your command?Temporarily modify your script so it always produces some visible output. For example, add the following right after the
#!/bin/sh
:and see what's in
/tmp/scheduleSpider.sh.log
after a few minutes. (You can set the command to run every minute so you don't have to wait as long for results.) If that works (it should), you can add moreecho
commands to your script to see in detail what it's doing.It looks like your script is designed to run only once; it creates the
sync.txt
file to prevent it from running again. That could be the root (ahem) of your problem. What that your intent? Did you mean to deletesync.txt
after running the command, and just forgot to do it?root
's home directory on Ubuntu is/root
. The first time your script runs, it should create/root/sync.txt
. Does that file exist? If so, how old is it?Note that
curl someLink
(assumingsomeLink
is a valid URL) will just dump the content from the specified link to standard output. Was that your intent (it will show up as e-mail toroot
? Or did you just not show us the entire command?第一:你可以用
*/5
替换第一个字段(参见 man 5 crontab)第二:通过输入
[电子邮件受保护]
在您的 crontab 中。如果脚本有任何输出,它将被邮寄。相反,您可能有一个本地邮箱,您可以在其中找到 cron 输出(通常是 $MAIL)。First: you can substitute the first field with
*/5
(see man 5 crontab)Second: have cron mail the output to your email address by entering
[email protected]
in your crontab. If the script has any output, it'll be mailed. Instead of that, you may have a local mailbox in which you can find the cron output (usually $MAIL).对 CRON 来说更好的语法是
另外,检查
scheduleSpider.sh
文件的权限。 Cron 在与您可能以交互方式执行程序的用户不同的用户下运行,因此 cron 可能没有权限。现在尝试 chmod 777 来检查一下。A better syntax for you CRON is
Also, check the authority of your
scheduleSpider.sh
file. Cron runs under a different user than the one you are likely executing your program interactively, so it may be that cron does not have authority. Trychmod 777
for now, just to check.我建议:
/scr_temp/scheduleSpider.sh
是否在脚本中正确设置了PATH
或使用命令的绝对路径 (/bin/touch< /code> 而不是
touch
)sync.txt
文件的绝对路径(或相对于脚本计算它)I suggest to:
/scr_temp/scheduleSpider.sh
has executable bitPATH
properly inside your script or use absolute path to command (/bin/touch
instead oftouch
)sync.txt
file (or calculate it relatively to script)您是通过 crontab -e 添加命令还是仅通过编辑 crontab 文件添加命令?您应该使用 crontab -e 来正确更新它。
Have you added the comand via
crontab -e
or just by editing the crontab file? You should usecrontab -e
to get it correctly updated.在 cron 脚本中设置工作目录,它可能不会执行您认为应该执行的操作。
Set the working directory in the cron script, it probably doesn't execute the things where you think it should.
您应该在脚本的绝对路径之前添加 /bin/sh 。
You should add /bin/sh before the absolute path of your script.