使用CRON作业执行PHP脚本

时间:2022-09-21 01:11:40

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.

我查看了很多帖子,但我找不到一个足够简单的例子供我学习和构建。

4 个解决方案

#1


22  

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.

这将在午夜每天执行。

#2


3  

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

如果你有一个sudo访问你的Linux服务器: - 然后执行以下操作

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,意味着0小时和0分钟

#3


1  

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

#4


0  

Are you using a company to host your website?

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

As you should have a icon in your c panel called cron jobs from there you can tell it what script to execute and when.

因为您的c面板中应该有一个名为cron jobs的图标,您可以告诉它要执行什么脚本以及何时执行。

#1


22  

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.

这将在午夜每天执行。

#2


3  

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

如果你有一个sudo访问你的Linux服务器: - 然后执行以下操作

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,意味着0小时和0分钟

#3


1  

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

#4


0  

Are you using a company to host your website?

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

As you should have a icon in your c panel called cron jobs from there you can tell it what script to execute and when.

因为您的c面板中应该有一个名为cron jobs的图标,您可以告诉它要执行什么脚本以及何时执行。