Overview
In Linux, crontab is a command that implements scheduled execution. Using this command, we can execute a certain PHP file regularly, thereby realizing automatic execution of scripts in Linux using PHP.
How to use PHP to automatically execute scripts in Linux?
When installing PHP, an executable file will be generated, the file name is php. Copy it to /usr/local/bin. Execute the php program in terminal mode:
php -q onlinnum.php
PHP was originally used in web applications, so it will send the HEADER of HTML. But here we are going to use PHP as a Shell Script. "-q" means not to send the HEADER. You can try it without adding -q to display the result. At this point you can already execute PHP code in terminal mode. Linux command: cron daemon
This is a resident service in the system. Its function is to perform routine work, such as checking the disk once a day or once a month. The cron daemon will check the scheduled work list (crontab) every minute to see if there are instructions to be executed, and all output will be sent to the user by mail.
Set crontab
Command: crontab -e
This command calls the vi editor to edit the execution list. For example
0 0 1,15 * * fsck /home
1 * * * * /home/myhome/printhello
Each line represents a scheduled job, before the command For the scheduled time, there are a total of 5 fields, separated by spaces, as follows from left to right:
-------------------- ------
minutes from 00 to 99
o'clock from 0 to 24
days from 01 to 31
months from 01 to 12
The week is from 01 to 07, representing Monday to Sunday
* means "every", if executed every day, fill in the * in the third column
-------- ------------------
So the two tasks in the above example are:
Check the /home disk on the 1st and 15th of every month