Linux regularly executes PHP files via cron (GO)

Source: Internet
Author: User

Linux regularly executes PHP files via cron

Additional points:1. To add the path of the interpreter to the PHP file header, usually #!/usr/bin/php2. Grant the PHP file execution permission to execute chmod a+x xx.php3. VI cronfileInput: 0 * * * * absolute path/xx.php >/dev/null 2>&14. Crontab cronfile5. If the error says Extension "path/xx.php" not presentIt is possible that the. php file was turned from windows, and needed to be executed Dos2unix, converted to UNIX format.  Cron is a timed execution tool under Linux that can run a job without human intervention. Because Cron is the built-in service of Linux, but it does not automatically up, you can manually start, shut down, restart the service, specific methods and paths to see the various systems;1.crontab CommandThe cron Service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:crontab-u//Set a user's Cron service, which is usually required by the root user when executing this commandcrontab-l//list details of a user cron servicecrontab-r//delete a cron service with no userscrontab-e//Edit a user's cron serviceFor example, do you want to add a file for cron execution on CRONTAB-E (the first time the selector editor?). )Put this in. * * * * * * * * * * /var/www/crontest.php, executed once every hour for 45 minutes. For example, root to view your cron settings: crontab-u root-lagain, for example, Root wants to delete Fred's cron settings: Crontab-u fred-rwhen editing the cron service, the edited content has some formatting and conventions, input: Crontab-u root-e2. Example/etc/crontab file configuration cronCron Service every minute not only to read all the files within/var/spool/cron, but also to read a/etc/crontab, so we configure this file can also use the Cron service to do something. The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. The file format for this file is:Shell=/bin/bashPath=/sbin:/bin:/usr/sbin:/usr/binMailto=root//If an error occurs, or if there is data output, the data is sent to this account as an emailhome=///user-run path, here is the root directory# Run-parts * * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly4 * * * Root run-parts/etc/cron.daily//daily execution of scripts within/etc/cron.daily4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthlyattention to the "run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name. Another example:# m H Dom Mon Dow command#每个小时的45分执行一次* * * * * /var/www/crontest.php parse the format of a row of commands! the first part of the command format is the time setting, the next part is the command to execute, if you want to execute too many commands, you can write these commands into a script, and then call this script directly here, you can recall the full path of the command when the call (yes, the above daily, This is the case with directory files such as weekly, which have a lot of script files to follow daily (daily). Time setting we have a certain agreement, the preceding five * number represents five numbers, the value range and meaning of the numbers are as follows:minutes (0-59) hours (0-23) Date (1-31) month (1-12) week (0-6)//0 representative SundayIt is the meaning of specifying a point in time, not every number of minutes, how many hours, if an hourly execution should be0 */1 * * */path/xx.php---it specifies the first 0 minutes of each hour to execute this fileor 0 * * * * * /path/xx.php and above are the same In addition to the numbers there are several special symbols are "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" represents from a number to a number, "," separate several discrete numbers. Here are a few more examples to illustrate the problem:every morning at 6.0 6 * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, no output is visible from the screen, because cron emails any output to root. every two hours, 0 minutes per two hours0 */2 * * echo "has a break now." >>/tmp/test.txtevery two hours between 11 o'clock and 8 in the morning, eight in the morning.0 23-7/2,8 * * * echo "a good Dream:)" >>/tmp/test.txtevery month, number 4th and Monday to Sunday, three a.m., 11 .0 4 * 1-3 command LineJanuary 1 morning, 4.0 4 1 1 * Command Line3, passing through the Apache Web serverPass the Apache Web server situation, this should pay attention to! (By the way, two methods of regular execution)when Bash runs a cron, it doesn't go through Apache, of course, with its parameters, include_paths, and so on. So, PHP will be in the/bin/local/php directory to find files, of course, can not find you are dumbfounded, all the include or require call other files failed; Workaround: Run the file through a browser such as lynx, links, w3m, and other text-mode browsers. If we had Lynx installed,13:52:[email Protected]/var/www$which Lynx/usr/bin/lynx Workaround:A,OK, here's the cron command (crontab-e) rewrite:* * * * * /var/www/crontest.php =* * * * * /usr/bin/lynx-source http://localhost/crontest.phpB,wordy, build a file. (/var/www/!/bin/sh/usr/local/bin/lynx-source http://localhost/crontest.phpB2 (CRONTAB-E)* * * * * /bin/sh/var/www/cron.shB,cron Regular execution of the SH to execute file, file and run crontest.php file via Lynxreally obviously, the B method wordy, but this is for a file, maybe the situation has advantages at different times, you say ...4. AttentionA, the file to be executed regularly with the ' x ' permission: chmod +x cron.cron B, file name and path to be guaranteed

Linux regularly executes PHP files via cron (GO)

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: 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.