Home > php教程 > PHP开发 > body text

Using php as linux automatic execution script

高洛峰
Release: 2016-12-02 10:45:46
Original
1450 people have browsed it

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 approach is, 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 complicated, it will significantly slow down the page. Normal calling speed.

Using this feature of PHP and adding the Linux crontab command, you can automatically execute a certain php file (counting the number of people online, etc.) at regular intervals.

Specific method:
When installing PHP, there will be An executable file named php. Copy it to /usr/local/bin.

Execute the php program in terminal mode: php -q onlinnum.php
Note that PHP was originally used on web pages Application? So it will send HTML HEADER by default? But here we want to use PHP as Shell Script? "-q" means not to send HEADER. You can try the display result without adding -q.

At this point you can already execute PHP code in terminal mode. Haha, you can try to execute the code you wrote before.

Linux command: cron daemon
This is a resident service in the system, its function is to execute examples Perform ongoing 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
                                                                                                                                                                                          ,以空格间隔,由左到右依序如下:
          栏位             说明
        --------------------------
          分钟          从00到99
From 0 to 24 从 from 01 to 31
from 01 to 12
from 01 to 07, it means on Monday to Sunday
*indicates "each". If it is executed every day, fill in the third column* The two tasks of the above example are:
Monthly 1st and 15th check/home disk
The first execution/Home/Gull/onlineNumber of each hour

View Crontab: Crontab -L & LT; ENTER & GT;
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, there are many missing values ​​of environment variables set in the system, which will cause problems when connecting to the database.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!