Executing a PHP script with a CRON Job [closed]

风格不统一 提交于 2019-12-27 20:13:10

问题


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.

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?

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


回答1:


Crontab needs the full path on your server.

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

This will execute every day at midnight.




回答2:


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

sudo crontab -e

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

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

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




回答3:


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)

You can use also @hourly special key:

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



回答4:


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.



来源:https://stackoverflow.com/questions/16144350/executing-a-php-script-with-a-cron-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!