簡體   English   中英

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

首先,確保腳本是可執行的:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM