Home  >  Article  >  Backend Development  >  [Transfer] PHP scheduled tasks: How to use Linux's Crontab to execute PHP scripts_PHP tutorial

[Transfer] PHP scheduled tasks: How to use Linux's Crontab to execute PHP scripts_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:27:57851browse

Redirect: https://www.centos.bz/2011/03/auto-run-task-crontab/

Our PHP program sometimes needs to be executed regularly. We can use the ignore_user_abort function or place js on the page to let the user help us implement it. But both methods are unreliable and unstable. We can use the Linux Crontab tool to trigger PHP execution tasks stably and reliably.
The two methods of Crontab are introduced below.

1. Use PHP to execute scripts in Crontab

Use a PHP program to call a PHP script just like calling a normal shell script in Crontab.
Execute myscript.php every hour as follows:

  1. # crontab -e
  2. 00 * * * * /usr/local/bin/php /home/john/myscript.php

/usr/local/bin/php is the path of the PHP program.

2. Use URL to execute script in Crontab

If your PHP script can be triggered by URL, you can use lynx or curl or wget to configure your Crontab.
The following example uses Lynx text browser to access a URL to execute a PHP script every hour. Lynx text browser uses conversational mode to open URLs by default. However, as shown below, we use the -dump option on the lynx command line to convert the URL output to standard output.

  1. 00 * * * * lynx -dump http://www.centos.bz/myscript.php

The following example uses CURL to access the URL to execute a PHP script every 5 minutes. Curl displays output on standard output by default. You can also dump the script's output to a temporary file using the "curl -o" option.

  1. */5 * * * * /usr/bin/curl -o temp.txt http://www.centos.bz/myscript.php

The following example uses WGET to access the URL to execute a PHP script every 10 minutes. The -q option indicates quiet mode. "-O temp.txt" means the output will be sent to a temporary file.

    1. */10 * * * * /usr/bin/wget -q -O temp.txt http://www.centos.bz/myscript.php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/815206.htmlTechArticleRedirected to: https://www.centos.bz/2011/03/auto-run-task-crontab/ Our PHP programs sometimes need to be executed regularly. We can use the ignore_user_abort function or place js on the page to let the user help...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn