Start with Cron, a timed execution tool for Linux. Users other than root can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in the /var/spool/cron directory and executed using the identity of the user who created them.

To create a crontab project as a user, log in as that user, and then type crontab -e to edit the crontab for that user. This file uses the same format as /etc/crontab. After the crontab changes are saved, the crontab file is saved according to the username and written to the /var/spool/cron/username file. The cron daemon checks every minute for changes in the /etc/crontab file, the /etc/cron. d/ directory, and the /var/spool/cron directory. If changes are found, they are loaded into memory. This way, you don’t have to restart the daemon when a crontab file changes.

Install the crontab:

yum install crontabs
Copy the code

Description:

/sbin/service crond start // Start the service
/sbin/service crond stop // Shut down the service
/sbin/service crond restart // Restart the service
/sbin/service crond reload // Reload the configurationRun the following command to check the crontab service status: service crond status To manually start the crontab service: service crond start To check whether the crontab service is enabled upon startup, run the ntsysv commandCopy the code

Add automatic startup after startup:

The chkconfig - level35 crond on
Copy the code

Execute myscript.php every hour as follows:

# crontab -e
00 * * * * /usr/local/bin/php /home/john/myscript.php
Copy the code

If your PHP script can be triggered by a URL, you can configure your Crontab using Lynx or curl or wget. The following example uses the Lynx text browser to access urls to execute hourly PHP scripts. Lynx text browser opens urls in dialog mode by default. However, as shown below, we use the -dump option on the Lynx command line to convert the output of the URL to standard output.

00 * * * * lynx -dump http://www.jb51.net/myscript.php
Copy the code

The following example uses CURL to access a URL to execute a PHP script every 5 points. Curl displays output in standard output by default. You can also dump the output of a script to a temporary file using the “curl -o” option.

* /5 * * * * /usr/bin/curl -o temp.txt http://www.jb51.net/myscript.php
Copy the code

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 indicates that the output is sent to a temporary file.

* /10 * * * * /usr/bin/wget -q -O temp.txt http://www.jb51.net/myscript.php
Copy the code

Parameter: -e Edits the timer Settings for the user. -l Lists the timer Settings for the user. -r Deletes the timer Settings of the user. -u< User name > Specifies the user name for which the timer is to be set.

Crontab format:

Basic format:

Minute hour day month week command


Column 1 indicates minutes 1 to 59 per minute. Column 2 indicates hours 1 to 23 (0 indicates point 0) Column 3 indicates dates 1 to 31 Column 4 indicates months 1 to 12 Column 5 Indicates week 0 to 6 (0 indicates Sunday) Column 6 Indicates the command to be run

Remember the meanings of the following special symbols: * (*) indicates the number within the value range, / (/) indicates each, – (-) indicates the number from a certain number to a certain number, and comma (,) separates several discrete numbers

Some examples of crontab files:

30 21* * * / usr/local/etc/rc. D/lighttpd restart the example above said every night21:30Restart apache.45 4 1.10.22* * / usr/local/etc/rc. D/lighttpd restart the example above said on a monthly basis1,10,22Day,4 : 45Restart apache.10 1 * * 6.0/ usr/local/etc/rc. D/lighttpd restart the example above said every Saturday and Sunday1 : 10Restart apache.0.30 18-23* * * / usr/local/etc/rc. D/lighttpd restart the example above said in a day18 : 00to23 : 00Between every30Minutes Restart the Apache.0 23 * * 6/ usr/local/etc/rc. D/lighttpd restart the example above said every Saturday11 : 00PM Restart the Apache.0* /1* * * / usr/local/etc/rc. D/lighttpd restart every hour to restart apache0 23-7/1* * * / usr/local/etc/rc. D/lighttpd restart at night11Point to the morning7Restart Apache every hour between points0 11 4* mon - wed/usr/local/etc/rc. D/lighttpd restart of the month4And every Monday through Wednesday11Point to restart apache0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
Copy the code

The above content hopes to help you, more PHP factory PDF, PHP advanced architecture video materials, PHP wonderful good article can be searched on wechat: PHP open source community

2021 Jinsanyin four big factory interview real questions collection, must see!

Four years of PHP technical articles collation collection – PHP framework

A collection of four years’ worth of PHP technical articles – Microservices Architecture

Distributed Architecture is a four-year collection of PHP technical articles

Four years of PHP technical essays – High Concurrency scenarios

Four years of elite PHP technical article collation collection – database