Cron工作没有开始

Upv*_*ote 5 shell ubuntu cron

我有一个我想要每5分钟执行一次的cron作业:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /scr_temp/scheduleSpider.sh
Run Code Online (Sandbox Code Playgroud)

/var/spool/cron/crontabs/root

cron应该执行shell脚本:

#!/bin/sh
if [ ! -f "sync.txt" ]; then
    touch "sync.txt"
    chmod 777 /scr_temp
    curl someLink
fi
Run Code Online (Sandbox Code Playgroud)

从命令行可以正常工作,但不能从cron工作.然而,cron本身是startet但脚本没有启动.

我读到了路径问题,但我真的不明白.我设置了一个将一些env数据写入文件的cron.这是输出:

HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
SHELL=/bin/sh
Run Code Online (Sandbox Code Playgroud)

如果我在命令行中执行env命令,我会得到以下PATH输出

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Run Code Online (Sandbox Code Playgroud)

我必须在shell脚本中设置什么路径?

Kei*_*son 4

你的$PATH很好;不要管它。在 Ubuntu 上,您调用的所有命令(touchchmodcurl)都位于/bin和/或中/usr/bin

你是如何设置 cron 作业的?你是以crontab some-fileroot 身份运行的吗?

这似乎/etc/crontab是作为 root 运行 cron 命令的常用机制。在我的 Ubuntu 系统上,sudo crontab -lno crontab for root. 以 root 身份运行crontab(就像使用任何非 root 帐户一样)应该没问题,但您可以考虑使用 root 身份/etc/crontab来代替。请注意,它使用与普通 crontab 不同的语法,如顶部注释中所述/etc/crontab

$ head -5 /etc/crontab
# /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.
Run Code Online (Sandbox Code Playgroud)

跑步sudo crontab -l。它显示你的命令吗?

暂时修改您的脚本,使其始终产生一些可见的输出。例如,在 后面添加以下内容#!/bin/sh

echo "Running scheduleSpider.sh at \`date\`" >> /tmp/scheduleSpider.sh.log
Run Code Online (Sandbox Code Playgroud)

/tmp/scheduleSpider.sh.log几分钟后查看内容。(您可以将命令设置为每分钟运行一次,这样您就不必等待很长时间才能得到结果。)如果有效(应该),您可以echo向脚本添加更多命令以详细查看它正在做什么。

看起来您的脚本被设计为只运行一次;它创建该sync.txt文件以防止其再次运行。这可能是你问题的根源(咳咳)。你的意图是什么?您是否想sync.txt在运行命令后删除,但忘记了?

rootUbuntu 上的主目录是/root. 脚本第一次运行时,它应该创建/root/sync.txt. 该文件存在吗?如果是的话,现在多大了?

请注意curl someLink(假设someLink是有效的 URL)只会将指定链接的内容转储到标准输出。这是您的意图吗(它将显示为电子邮件root?或者您只是没有向我们展示整个命令?