当我访问URL时,我的php脚本没有运行,而是通过Cron Job在预定时间运行

时间:2023-01-19 01:11:35

Others have asked similar questions, but from what I understand their scripts are automated, but when you go to www.mydomain.com/myscript.php, the script still runs.

其他人提出了类似的问题,但据我所知,他们的脚本是自动化的,但是当你访问www.mydomain.com/myscript.php时,脚本仍会运行。

Or at least that's how it was for me when I set up a cron job in the cpanel for my script. Is there any way I can make it so the script doesn't run when I access that URL, but rather is static, except for its scheduled updates?

或者至少就是我在cpanel中为我的脚本设置一个cron作业时的情况。有没有什么方法可以让我这样做,所以当我访问该URL时脚本不会运行,而是静态,除了它的预定更新?

I've seen solutions where the script itself is edited to check the last time it ran, and if it was less than X hours ago, it presents that log file.
The problem is I don't know where to find that log file. I'm using BlueHost, if that matters.

我已经看过编译脚本本身的解决方案,以检查它上次运行的时间,如果它不到X小时前,则会显示该日志文件。问题是我不知道在哪里找到该日志文件。我正在使用BlueHost,如果这很重要的话。

1 个解决方案

#1


You could add something like if ($argv[1] == 'RUN') at the top of your php script (before it starts executing your stuff).

您可以在php脚本的顶部添加if($ argv [1] =='RUN')之类的东西(在它开始执行你的东西之前)。

Then you just need to pass that parameter in your cron command, i.e.: 4 4 * * * /usr/bin/php /var/www/mydomain.com/myscript.php RUN to run at 04:04 every day (n.b. easy webtool to check your cron statement is correct - http://cronchecker.net/)

然后你只需要在你的cron命令中传递该参数,即:4 4 * * * / usr / bin / php /var/www/mydomain.com/myscript.php RUN每天04:04运行(nb easy webtool检查你的cron语句是否正确 - http://cronchecker.net/)

This way, you can keep your script where it is, but it won't execute when you browse to it.

这样,您可以将脚本保持在原位,但在浏览时不会执行。

#1


You could add something like if ($argv[1] == 'RUN') at the top of your php script (before it starts executing your stuff).

您可以在php脚本的顶部添加if($ argv [1] =='RUN')之类的东西(在它开始执行你的东西之前)。

Then you just need to pass that parameter in your cron command, i.e.: 4 4 * * * /usr/bin/php /var/www/mydomain.com/myscript.php RUN to run at 04:04 every day (n.b. easy webtool to check your cron statement is correct - http://cronchecker.net/)

然后你只需要在你的cron命令中传递该参数,即:4 4 * * * / usr / bin / php /var/www/mydomain.com/myscript.php RUN每天04:04运行(nb easy webtool检查你的cron语句是否正确 - http://cronchecker.net/)

This way, you can keep your script where it is, but it won't execute when you browse to it.

这样,您可以将脚本保持在原位,但在浏览时不会执行。