gpt4 book ai didi

php - Linux cron 任务 - 如何添加和运行 cron 任务?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:07:48 27 4
gpt4 key购买 nike

如何在 Linux 中运行 cron 任务?

关注此Q&A ,我有这个 cron 任务要运行 - 只是将一些信息写入 txt 文件,

// /var/www/cron.php
$myfile = fopen("cron.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);

但是在通过我的终端添加 cron 任务之后,

crontab -e

然后,

* * * * * /usr/bin/php /var/www/cron.php &> /dev/null

但是当我打开cron.txt的时候,里面什么都没有。

为什么?我错过了什么?

最佳答案

通过完整路径/var/www/my_system/cron.txt更改cron.txt

// /var/www/cron.php
$myfile = fopen("/var/www/my_system/cron.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);

或者移动到目录:

chdir("/var/www/my_system");
$myfile = fopen("cron.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);

然后再试一次。

关于php - Linux cron 任务 - 如何添加和运行 cron 任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35168492/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com