Cron 作业在 Raspberry 中不起作用

Cron job not working in Raspberry

我想在我的 Raspberry 中添加一个 cron 作业以每五分钟执行一次任务。 所以我在终端做:

crontab -e

然后在文件中添加:

*/1 * * * * /usr/bin/php myscript path.

脚本非常简单。只是试试它是否有效:

 <?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = date("l jS \of F Y h:i:s A") . "<br>";;
fwrite($myfile, $txt);
fclose($myfile);
?>

问题是日期未更新,因此 cron 作业不起作用。知道这个问题吗?

更新

这是我在执行 crobtab -e 时得到的结果

    GNU nano 2.2.6        File: /tmp/crontab.3IXg0z/crontab                       

# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
* * * * * /usr/bin/php /full/path/myscript.php

如果您想每 5 分钟 运行 一个脚本,您应该添加此条目。

*/5 * * * * /usr/bin/php /full/path/to/php/script.php

首先,确保脚本是可执行的:

chmod +x /path/to/some/script.php

其次,确保您的脚本在第一行有适当的 #!(或 "shebang"):

#!/usr/bin/php

然后确保您的 cron 作业配置正确。 cron 的格式通常是 m h dom mon dow command

sudo crontab -e

*/5 * * * * /path/to/some/script.php

确保您在 crontab 上正确设置了 PATH 变量,以便它可以找到您的文件。

您可以简单地将以下行放在 crontab

的顶部
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/path/to/newfile.txt

试试,下面测试

* * * * * touch /tmp/hello

执行以下操作以重定向结果

*/5 * * * * /usr/bin/php /full/path/to/php/script.php > /tmp/out.txt

确保您的脚本 运行 在命令行上。

/usr/bin/php /full/path/to/php/script.php

使用-f选项执行脚本:

*/5 * * * * /usr/bin/php -f /full/path/to/php/script.php

尾日志文件以查看其每 5 分钟执行一次

tail -f /var/log/cron