Home > Backend Development > PHP Tutorial > How to use php as a Linux automatic execution script_PHP tutorial

How to use php as a Linux automatic execution script_PHP tutorial

WBOY
Release: 2016-07-13 17:27:58
Original
1005 people have browsed it

Overview
In Linux, crontab is a command that implements scheduled execution. Using this command, we can execute a certain PHP file regularly, thereby realizing automatic execution of scripts in Linux using PHP.
How to use PHP to automatically execute scripts in Linux?
When 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
PHP was originally used in web applications, so it will send the HEADER of HTML. But here we are going to use PHP as a Shell Script. "-q" means not to send the HEADER. You can try it without adding -q to display the result. At this point you can already execute PHP code in terminal mode. Linux command: cron daemon
This is a resident service in the system. Its function is to perform routine work, 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 execution list. For example
0 0 1,15 * * fsck /home
1 * * * * /home/myhome/printhello
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:
-------------------- ------
minutes from 00 to 99
o'clock from 0 to 24
days from 01 to 31
months from 01 to 12
The week is from 01 to 07, representing Monday to Sunday
* means "every", if executed every day, fill in the * in the third column
-------- ------------------
So the two tasks in the above example are:
Check the /home disk on the 1st and 15th of every month


Execute the file /home/myhome/printhello at the first minute of every hour
View crontab: crontab -l
Delete crontab: crontab -r
We will Now you know how to use PHP and crontab commands to automatically execute scripts in Linux.
What else should we pay attention to?
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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531814.htmlTechArticleOverviewIn Linux, crontab is an instruction to implement scheduled execution. Using this instruction, we can execute certain tasks regularly. A php file to realize automatic execution of scripts in Linux using PHP. ...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template