简体   繁体   English

Cron作业无法在Raspberry中运作

[英]Cron job not working in Raspberry

I want to add a cron job in my Raspberry to execute a task every five minutes. 我想在Raspberry中添加一个cron作业,以每五分钟执行一次任务。 So I do in the terminal: 所以我在终端上做:

crontab -e

and then add at the file: 然后添加文件:

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

The script is something very simple. 该脚本非常简单。 just to try if it works: 只是尝试它是否有效:

 <?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);
?>

The problem is that the date isn't updated, so the cron job doesn't work. 问题在于日期未更新,因此cron作业无法正常工作。 Any idea of the problem ? 有什么问题的想法吗?

UPDATE 更新

This what I got, when I execute crobtab -e 这是我执行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

First, make sure the script is executable: 首先,确保脚本是可执行的:

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

Second, make sure your script has an appropriate #! 其次,确保您的脚本具有适当的#! (or "shebang" ) on the very first line: (或“ shebang” )在第一行:

#!/usr/bin/php

Then make sure your cron job is configured correctly. 然后,确保您的cron作业配置正确。 The format for cron is typically mh dom mon dow command cron的格式通常是mh dom mon dow command

sudo crontab -e

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

如果要每5分钟运行一次脚本,则应添加此条目。

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

Make sure you have PATH variable set correctly on crontab, so it can locate your file. 确保在crontab上正确设置了PATH变量,以便它可以找到您的文件。

You can simple put following line in top of crontab 您可以简单地将以下行放在crontab顶部

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

Try, following test 试试,下面的测试

* * * * * touch /tmp/hello

Do following to redirect result 执行以下操作以重定向结果

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

Make sure your script running on command line. 确保您的脚本在命令行上运行。

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

Use the -f option to execute the script: 使用-f选项执行脚本:

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

Tail logs file to see its executing every 5 min 尾部日志文件以查看每5分钟执行一次

tail -f /var/log/cron

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM