Use php for linux automatic script execution

Source: Internet
Author: User
Using php for linux automatic execution scripts, reading using php for linux automatic execution scripts, using PHP for a community, when writing to calculate the maximum number of online users, after successful debugging, PHP is run directly on the server as shellscript. when working in the community, it is often necessary to count the number of online users and other data. generally, put this code in the user's login or a page. "> <LINKhref

Using PHP as a community, when writing to calculate the maximum number of people going online, we successfully debugged PHP as a shell
The script runs directly on the server.

When working in the community, you often need to count the number of people online and other data. the general practice is to put this code in the user

Login or a page to trigger this code when the user logs on or accesses the page.

This will cause a problem. if the code is complicated, it will obviously slow down the normal call speed of the page.

 

With this feature of PHP and the linux crontab command, you can automatically and periodically execute a php file.

(Count the number of online users ).

 

Specific method:

When installing PHP, an executable file is generated. the file name is php. copy it to/usr/local/bin.

Bottom.

 

Run the php program in the terminal mode: php-q onlinnum. php

Note that PHP is originally used in web applications, so it will certainly send HTML

HEADER, but here we want to use PHP as a Shell Script, "-q" means not to send

HEADER. you can try not to add the-q display result.

 

Now you can run the PHP code in the terminal mode.

Code.

 

Linux command: cron daemon

This is a resident service in the system and is used to perform routine work, such as once a day or once a month.

Disk. Cron daemon checks the scheduled worksheet (crontab) every minute.

Check whether there are any commands to be executed. all the output will be sent to the user via mail.

 

Set crontab

Command: crontab-e

This command calls the vi editor to edit the execution list. For example

& Nbs p; 0 0 ** fsck/home

& Nbs p; 1 *****/home/Gull/onlinnumber

Each row represents a scheduled task. it is scheduled before the command. there are 5 columns in total, with spaces

Intervals, in ascending order from left to right:

Field & nbs p; description

--------------------------

Minutes from 00 to 99

From 0 to 24 o'clock

Day & nbs p; from 01 to 31

Month & nbs p; from 01 to 12

The week ranges from 01 to 07, representing Monday to Sunday.

* Indicates "every". if it is executed every day, enter * in the third column.

So the two tasks in the above example are:

Check/home disk on the 1st and 15th of each month

Run the/home/Gull/onlinnumber file in the first hour.

 

View crontab: crontab-l

Delete crontab: crontab-r

 

Well, here, you should know how to use the PHP and crontab commands to count the number of online users?

 

Note:

If a database is used in php, such as oracle, the export Oracle environment is also required in the execution script.

. Because the script execution in cron is performed by users without logon, many systems are missing.

The set environment variable value will be asked when connecting to the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.