使用 CRON 作业执行 PHP 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16144350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:36:32  来源:igfitidea点击:

Executing a PHP script with a CRON Job

phpautomationcron

提问by djnetherton

I would like to run a PHP script every day at midnight. After research on how to do this, it appears that the best way to achieve this is to use a CRON job.

我想每天午夜运行一个 PHP 脚本。在研究如何做到这一点之后,似乎实现这一目标的最佳方法是使用 CRON 作业。

If my php script was located at http://example.com/scripts/scriptExample.php, can somebody be able to show the most simple example of what this CRON command would look like?

如果我的 php 脚本位于http://example.com/scripts/scriptExample.php,有人能够展示这个 CRON 命令的最简单示例吗?

I have looked through numerous posts but I cannot find a simple enough example for me to learn and build upon.

我浏览了许多帖子,但找不到一个足够简单的示例供我学习和构建。

回答by karmafunk

Crontab needs the full path on your server.

Crontab 需要您服务器上的完整路径。

0 0 * * * php /var/www/vhosts/domain.com/httpdocs/scripts/example.php

This will execute every day at midnight.

这将在每天午夜执行。

回答by Nishant

If You have a sudo access to your linux server :- Then do the following

如果您有对 linux 服务器的 sudo 访问权限:-然后执行以下操作

sudo crontab -e

This will open the cron tab for you on your server.

这将在您的服务器上为您打开 cron 选项卡。

Next thing is you have to do a cron entry for the file which you want to execute

接下来是你必须为要执行的文件做一个 cron 条目

00 00 * * * /usr/local/bin/php "path of the php file which you want to execute"

00 00 * * *this will run your cron at midnight daily, means at 0hrs and 0mins

00 00 * * *这将在每天午夜运行你的 cron,意味着在 0hrs 和 0mins

回答by Adidi

So something like this:

所以像这样:

00 * * * * /usr/local/bin/php /home/john/myscript.php

The 00 * * * * means hourly /usr/local/bin/php - where php main engine is in /home/john/myscript.php - the script to run (physical path)

00 * * * * 表示每小时 /usr/local/bin/php - php 主引擎在 /home/john/myscript.php - 要运行的脚本(物理路径)

You can use also @hourly special key:

您也可以使用@hourly 特殊键:

@hourly /usr/local/bin/php /home/john/myscript.php

回答by Chris par

Are you using a company to host your website?

您是否使用公司来托管您的网站?

As you should have a icon in your c panelcalled cron jobsfrom there you can tell it what script to execute and when.

因为你应该有一个从那里c panel调用的图标,cron jobs你可以告诉它要执行什么脚本以及何时执行。