Home >Backend Development >PHP Tutorial >Use PHP to automatically execute scripts in Linux (haha, super easy to use function)_PHP tutorial
Recently, I am using PHP to build a community. When I wrote about calculating the maximum number of people online, I successfully debugged and used PHP as a shell script to run directly on the server.
When building a community, I often need to count the number of people online and other data. The general method is as follows , put this code in the user login or a certain page, so that when the user logs in or accesses the page, the code will be triggered to run. This will cause a problem. If the code is complex, it will significantly slow down the page. The normal calling speed.
Using this feature of PHP and adding the Linux crontab command, you can automatically execute a certain PHP file at regular intervals (counting the number of people online, etc.).
Specific method:
Install PHP When , 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
Note that PHP was originally used in web applications, so it will send the HTML HEADER by default. But here we are going to use PHP as a Shell Script. "-q" means not to send the HEADER. You can try it. Add -q to display the results.
At this point you can already execute PHP code in terminal mode. Haha, you can try executing the code you wrote before.
Linux command: cron daemon
This is a resident service in the system. The function is to perform routine tasks, 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 executed list. For example,
0 0 1,15 * * fsck /home
1 There are a total of 5 fields, separated by spaces, as follows from left to right:
Fields Description
-------------------------- ----
Minutes From 00 to 99
O’clock From 0 to 24
Day From 01 to 31
Month From 01 to 12