如何每天执行一个PHP脚本 [英] How to execute a php script every day

查看:174
本文介绍了如何每天执行一个PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Ubuntu for php上的Cron作业

运行和ubuntu服务器,并希望每天运行一个php脚本。我做了一些研究,发现cron是这样做的最好的方法,这是我被困,在互联网上的很多信息关于cron很难跟随和理解。

I am running and ubuntu server and wanted to run a php script every day. I have done some research and found that cron is the best way of doing this however, this is where i got stuck, a lot of the information on the internet about cron is very hard to follow and understand.

所以我想每天执行一个简单的php脚本,我做的测试脚本只是从数据库中删除一个记录,但真正的脚本会做更多。

So i wanted to execute a simple php script once a day, the script i made for testing simply just deletes a record from a database, but the real script will do a lot more.

我尝试通过我的web主机服务通过plesk设置任务,但它似乎并没有执行,当我想要,我使用1分钟,22小时,*为星期,*为星期,*为月,并认为这将在每天22:01执行。

I tried setting up a task through plesk which is provided through my web host service but it didn't seem to execute when i wanted it to, i used 1 for minutes, 22 for hours, * for day, * for week and * for month and thought this would execute every day at 22:01.

我有我的服务器上的目录:
cron.hourly
cron.daily
cron.weekly
cron.monthly

I have the directories on my server: cron.hourly cron.daily cron.weekly cron.monthly

我想我可以在那里转储i文件它会执行例如每一天,但我猜我需要一个cron脚本调用一个php脚本吧?

I thought i could dump i file in there and it would execute for example every day, but i'm guessing i need to make a cron script to call a php script right?

如果我去的方式将文件放在cron.daily文件夹中我该怎么办?

If i were to go the way of putting a file in the cron.daily folder how would i go about it?

此外,如果有任何步骤,我需要采取PHP的一面,请让我知道吗?

Also if there are any steps i need to take on the php side please let me know?

感谢您的时间。

推荐答案

设置cron作业的方法。假设你有shell访问,你可以从控制台 crontab -e 中定义作业,例如:

There's couple of ways to setup cron job. Assuming you got shell access you could do crontab -e from console and define job there, i.e. like this:

1 22 * * * command

$ c>命令(无论是什么)在22:01每天(不知道为什么你设置分钟 1 而不是 0 虽然)。要从那里启动PHP脚本,您必须安装 php-cli ,然后按照以下方式调用:

which would trigger command (whatever it is) at 22:01 each day (not sure why you set minutes to 1 instead of 0 though). To launch PHP script from there you would either have to install php-cli, and then invoke it that way:

1 22 * * * <path>/php -q script.php

你还可以在这里调用bash脚本,设置所有的东西,如路径等,然后调用你的php脚本形式bash - 有时它更容易做到这一点,而不是制作太长的命令行cron。稍后更新它更简单。也可以通过设置它的执行位( chmod a + x script.php )并在你的脚本中添加shell行作为第一行,将你的php脚本转换为bash-runnable脚本:

You can also call bash script here, to setup all the stuff like paths etc and then call your php script form bash - sometimes it is simpler to do that way instead of crafting way too long command line for cron. And it's simpler to update it later. also, you could turn your php script into bash-runnable script by setting it execution bit (chmod a+x script.php) and adding shell line as 1st line in your script:

#!/usr/bin/php -q
<?php
   ...

如果你的脚本有太多的依赖,你更喜欢通过web调用它, wget 以模仿浏览器。因此您的命令将是:

If your script got too many dependencies and you'd prefer to call it via web, you could use wget to mimic a browser. so your command would be:

/usr/bin/wget --delete-after --quiet --spider <URL-TO-YOUR-SCRIPT>

wget手册可以通过 man wget wget -h ,或者是在此网站上。或者,你可以使用 HEAD 从perl-www包,但它需要perl,而wget是独立工具。如果您使用带自签名证书的HTTPS,请添加 - no-check-certificate 。您还可以设置 .htaccess 并限制对cron脚本的访问localhost / 127.0.0.1

wget manual can be accessed by man wget or wget -h, or is on this website. Aternatively you may use HEAD from perl-www package but it requires perl while wget is standalone tool. If you use HTTPS with self signed certs add --no-check-certificate. And you may also want to setup .htaccess and limit web access to your cron script to localhost/127.0.0.1

这篇关于如何每天执行一个PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆