In PHP + LINUX, how does one implement the scheduled service and what tools are used on LINUX ?, How to use php for Linux automatic script execution? Summary In Linux, crontab is a command to implement scheduled execution. with this command, we can regularly execute a PHP file to implement automatic execution of scripts in Linux using PHP. How to use PHP for Linux automatic script execution? How to implement scheduled service in PHP + LINUX
What tools are used on LINUX?
------ Solution --------------------
How to use php for Linux automatic script execution?
Summary
In Linux, crontab is a command for scheduled execution. with this command, we can regularly execute a PHP file to implement automatic execution of scripts in Linux using PHP.
How to use PHP for Linux automatic script execution?
When installing PHP, an executable file is generated. the file name is php, which is usually in the/php/bin/php installation path. . Copy it to/usr/local/bin. run the php program on the terminal:
Php-q onlinnum. php
PHP was originally used for webpage applications, so it will send the html header, but here we want to use PHP as a Shell Script, "-q" means not to send the HEADER. you can try not to add the result of-q. Now you can execute PHP code in the terminal mode. Linux command: cron daemon
This is a resident service in the system that performs routine work, such as checking disks once a day or once a month. Cron daemon checks the scheduled worksheet (crontab) every minute to see if there are instructions to be executed. all outputs are sent to the user by mail.
Set crontab
Command: crontab-e
This command calls the vi editor to edit the execution list. For example
Each row represents a scheduled task. it is a scheduled time before the command. There are a total of five columns separated by spaces, sorted from left to right as follows:
--------------------------
Minutes from 00 to 99
From 0 to 24 o'clock
From 01 to 31
From 01 to 12 months
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/myhome/printhello file in the first hour.
View crontab: crontab-l
Delete crontab: crontab-r
We will know how to use the PHP and crontab commands to execute scripts automatically in Linux.
What else do you need to pay attention?
If a database is used in php, such as oracle, you also need to set the export Oracle environment in the execution script. Because the script execution in cron is performed without logon, many environment variable values set in the system are missing, and problems may occur when connecting to the database.
This can also be done in the command line.
/Usr/local/webserver/php/bin/php-q path/a. php is the php installation path, followed by the php file name and path to be executed
------ Solution --------------------
1st floor
PHP is the script language on the server. if you want to regularly execute some programs, there is no convenient way.
On the Linux server, crontab can stably execute PHP scripts on a regular basis.
Crontab is used to allow users to execute programs at a fixed time or interval.
On the linux platform, you can write a cron script to implement the task scheduling function.
Execute tasks at a certain frequency
By default, the crond process is started in linux. the crond process does not need to be started or closed.
The crond process reads and executes scheduling tasks. you only need to write the corresponding scheduling script to the cron scheduling configuration file.
Cron scheduling files include the following:
If the task is not executed in hourly monthly weekly mode, you can write the corresponding crontab to the crontab or cron. d Directory.
Example:
Run the script every minute/opt/bin/test-cron.sh
You can create a script echo-date.sh in cron. d
Content is
*/1 *** root/opt/bin/test-cron.sh
Execute PHP scripts
Method 1
If you want to regularly execute a PHP script, you can directly write it like this:
*/1 * root-q/bin/local/php/path/to/your/php/I love douding. php (I love douding. php every minute)
Note: In this case, PHP needs to be converted into executable script files. In the command line, the directory where the script is located
Chmod + x I love douding. php
Method 2
The above method has a problem: when I love the douding script to include the file, the script cannot find the PHP path at all. php will find the file in the/bin/local/php directory, of course not found.
In this case, follow these steps:
Enter the following command on the terminal to obtain the path of the sh and lynx programs:
Whereis sh
Whereis lynx
Or
Which sh
Which lynx
If you enter one, the path you want will be returned. If your system does not have lynx, try links, w3m, or links2. these are other text model browsers. These browsers will run your scripts.
Then you need to create a sh file and use it to call the php file through lynx. Assume that your server has installed lynx, the sh path is/bin/sh, and the lynx path is/usr/local/bin/lynx.
#! /Bin/sh
/Usr/local/bin/lynx-source http://www.yoursite.com/folder/ .php
Note the exclamation point
Save this script as a cron-lynx.sh
Save it to a location you can find, for example:
You can put it in the root directory of the website.
Create another file that I love douding. php. When cron is running, it will be called through lynx. This file can be accessed through a browser.
Then edit the crontab of the host, as shown below.
*/1 * root/bin/sh/usr/www/yourdirectory/sh _ I love s/cron-lynx.sh
There is a space between/bin/sh and/usr. In this way, you can set up your sh file and PHP file correctly.
Example 1
Regularly execute PHP scripts in linux Write PHP in normal mode
$ File = '/htdocs/www/b100/egspace/Bin/test/'.date('h'{.'-'date(' I '{.'-'.date('s'{.'.txt ';
File_put_contents ($ file, date ('Y-m-D '));
?>
Change the php file attribute to a shell file:
Chmod + x test. php
Edit crontab again:
Crontab-e
Add one line:
*/1 *****/usr/local/bin/php-q/htdocs/www/b100/egspace/Bin/test. php
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.