How do I use Linux's Crontab method to periodically execute PHP scripts

  • 2020-05-10 17:52:58
  • OfStack

Two approaches to Crontab are described below.

1. Use PHP in Crontab to execute scripts

Just as normal shell script 1 is called in Crontab (specific Crontab usage), the PHP program is used to call the PHP script.
myscript is executed every 1 hour.php is as follows:
 
# crontab -e 
00 * * * * /usr/local/bin/php /home/john/myscript.php 

/ usr local/bin php as PHP application path.

2. Execute the script using URL in Crontab

If your PHP script can be triggered via URL, you can configure your Crontab using lynx or curl or wget.
The following example USES the Lynx text browser to access URL to execute PHP scripts per hour. The Lynx text browser opens URL by default in dialog mode. However, as shown below, we use the -dump option on the lynx command line to convert the output of URL to standard output.
 
00 * * * * lynx -dump //www.ofstack.com/myscript.php 

The following example USES CURL to access URL to execute PHP scripts every 5 cents. Curl displays output on standard output by default. Using the "curl-o" option, you can also dump the output of the script to a temporary file.
 
*/5 * * * * /usr/bin/curl -o temp.txt //www.ofstack.com/myscript.php 

The following example USES WGET to access URL to execute PHP scripts every 10 cents. -q option for quiet mode." -O temp.txt "means that the output will be sent to a temporary file.
 
*/10 * * * * /usr/bin/wget -q -O temp.txt //www.ofstack.com/myscript.php 

Related articles: