简体   繁体   English

如何在php中运行cron作业

[英]How to run cron job in php

I have php function that I wnat to run every 15 minutes. 我拥有php函数,每15分钟运行一次。 I found some questions like this: 我发现了一些这样的问题:

How can I make a cron to execute a php script? 我如何使Cron执行php脚本?

and in my case I should use this: 在我的情况下,我应该使用:

*/15 * * * * /usr/local/bin/php -q /path/to/my/file.p

But should I run this command in terminal or put it in my file? 但是我应该在终端中运行此命令还是将其放入文件中? And once, it is executed, will it run all the time or will have time limit? 一旦执行,它会一直运行还是有时间限制?

Thanks! 谢谢!

PHP doesn't run cron jobs, your server (or operating system) is doing this. PHP无法运行cron作业,您的服务器(或操作系统)正在执行此操作。 There are two ways to put your cron job to work: 有两种方法可以使您的cron工作正常工作:

#1 #1

Using the shell command crontab . 使用shell命令crontab The command crontab -l will list all existing cronjobs for your user (most likely there are none yet). 命令crontab -l将为您的用户列出所有现有的cronjob(很可能还没有)。 crontab -e will open an editor window where you can put in a you cron job as a new line. crontab -e将打开一个编辑器窗口,您可以在其中放置cron作业作为新行。 Save, and your cron job is now running. 保存,您的Cron作业现在正在运行。 crontab -l again and you will see it listet. 再次使用crontab -l ,您将看到它的清单。 crontab -r to remove all the cont jobs. crontab -r删除所有续作业。

You can also start a cron job from a file. 您也可以从文件开始cron作业。 Simply type crontab filename (eg. crontab textfile.txt ) 只需键入crontab filename (例如crontab textfile.txt

Alternatively you can also start it from within PHP. 另外,您也可以从PHP中启动它。 Just put your cron job into a file and start it via exec() like so: 只需将您的cron作业放入文件中,然后通过exec()启动它,如下所示:

file_put_contents( 'textfile.txt', '*/15 * * * * /usr/local/bin/php -q /path/to/my/file.php' );
exec( 'crontab textfile.txt' );

#2 #2

If you have admin privileged on your system you can create a file in /etc/cron.d/ (for example, call it my_cronjob ) and put your cron job there. 如果您具有系统管理员权限,则可以在/etc/cron.d/创建一个文件(例如,将其my_cronjob ),然后将cron作业放在该文件中。 In this case you probably want to run it as a user (not as admin, that would be rather insecure). 在这种情况下,您可能希望以用户身份运行它(而不是以admin身份运行,那会很不安全)。 This is quite easy to do, just add the user name like so: 这很容易做到,只需添加用户名即可:

*/15 * * * * user_name /usr/local/bin/php -q /path/to/my/file.p

(In this case the cron job will not be listet under crontab -l ) (在这种情况下,cron作业将不会在crontab -l


Answering your second question: As long as the cron job is listet in crontab -l or as long as the file is sitting in /etc/cron.d the cron job is running, in your case, every 15 minutes. 回答第二个问题:只要cron作业是crontab -l中的列表文件,或者只要文件位于/etc/cron.d中,就您的情况而言,cron作业每15分钟运行一次。

10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1 There are two main parts: 10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1有两个主要部分:

The first part is "10 * * * *". 第一部分是“ 10 * * * *”。 This is where we schedule the timer. 这是我们安排计时器的地方。 The rest of the line is the command as it would run from the command line. 该行的其余部分是命令,因为它将从命令行运行。 The command itself in this example has three parts: 在此示例中,命令本身包含三个部分:

"/usr/bin/php". “在/ usr / bin中/ PHP的”。 PHP scripts usually are not executable by themselves. PHP脚本通常本身不能执行。 Therefore we need to run it through the PHP parser. 因此,我们需要通过PHP解析器运行它。 "/www/virtual/username/cron.php". “/www/virtual/username/cron.php”。 This is just the path to the script. 这只是脚本的路径。 "> /dev/null 2>&1". “> / dev / null 2>&1”。 This part is handling the output of the script. 这部分正在处理脚本的输出。 More on this later. 稍后对此进行更多讨论。

Please read this tutorial http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800 请阅读本教程http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM