[英]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
首先,确保脚本是可执行的:
chmod +x /path/to/some/script.php
其次,确保您的脚本具有适当的#!
(或“ shebang” )在第一行:
#!/usr/bin/php
然后,确保您的cron作业配置正确。 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
确保在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.