Home > Article > Backend Development > Using PHP to automatically execute scripts in Linux_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:
After 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
Note that PHP was originally used in web applications, so it will send the HTML HEADER by default. But here we are using PHP as a Shell Script. "-q" means not to send the HEADER. You can try it or not. 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
&nbs p; 0 0 1,15 * * fsck /home
&nbs p; 1 * * * * /home/Gull/onlinnumber
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:
Field&nbs p; Description
------------- --------- ----
Minute from 00 to 99
Hour from 0 to 24
Day&nbs p;from 01 to 31
Month&nbs p ;From 01 to 12
The week is from 01 to 07, representing Monday to Sunday
The * sign means "every", if it is executed every day, fill in the * sign in the third column
So the two items in the above example The job is:
Check the /home disk on the 1st and 15th of every month
Execute the file /home/Gull/onlinnumber at the first minute of every hour
View crontab: crontab -l
Delete crontab: crontab -r
Haha, at this point, you should know how to use PHP and crontab commands to count the number of people online, right?
Note:
If a database is used in php, such as Oracle, etc., the Oracle environment settings need to be exported in the execution script. Because the scripts executed under cron are executed by users who are not logged in, many values of environment variables set in the system are missing, which may cause problems when connecting to the database.