Usage of crontab in Ubuntu

Source: Internet
Author: User

Usage of crontab in Ubuntu

If a php script needs to be run at a specified time, such as regular website data statistics, automatic Database Backup tasks, and timed email sending.

If we do not use Cron to execute php scripts, we may use the php include method.

For example, add the code in index. php: <? Php include "crontab. php"?>

When a visitor accesses http: // xxxx/index. php through a browser, crontab. php is automatically executed.

Of course, this is just a way of thinking. This method does not allow you to run the PHP script at a specified time, but also brings additional server consumption to index. php. Linux Ubuntu Crontab can well complete php scheduled tasks.


CGI Module


If the Server API is cgi, add additional code to the first line of the php script that requires cron execution.

#! /Usr/bin/php-q (similar to the PERL syntax), specifying the location of the php executable program on the server

The procedure is as follows:

  1. Make sure that the Ubuntu system has installed the php5-cli module installation command: apt-get install php5-cli. Generate php executable programs under the/usr/bin/php Directory.

    2. Edit the php script file crontab. php to be executed when the task is to be determined, for example:

    1234 #!/usr/bin/php -q<?php $file = "/var/www/".date('H').'_'.date('i').'_'.date('s').'.txt'; file_put_contents($file,date('Y-m-d'));

    3. edit a crontab task:

    Open the terminal and enter the following command: crontab-e,

    Select an editor. To change later, run 'select-editor '.

    1./bin/ed

    2./bin/nano <---- easiest

    3./usr/bin/vim. tiny

    Select an Ubuntu editor and edit the/tmp/crontab. AuN7IQ/crontab file:

    # M h dom mon dow command

    Add the scheduled command below:

    * *** Php/path/to/your/cron. php

    The five asterisks indicate the timing of each minute, hour, day, month, and week. For example, run crontab. php at seven o'clock P.M. every day. The command is:

    * 19 *** php/var/www/cron/crontab. php

    Then press Ctrl + O to save and Ctrl + X to exit.


    Apache module


    If the php installation runs in apache mode, you do not need to add # At the beginning of the php script scheduled to be executed on the ubuntu server #! /Usr/bin/php-q. The php script is scheduled to run in ubuntu through crontab and automatically accessed through the Lynx browser.

    1. Install the Lynx browser.

    Lynx is a lightweight web browser used in most Unix and Linux systems.

    Installation command: sudo apt-get install lynx

    2. Edit the cron task and run the crontab-e command.

    * *** Lynx-dump http://xxxx.com/cron.php

    Note that it is best to use an absolute address that contains the complete url of "http.


    Use CURL to complete remote calls


    1. Create a scheduled task and run the crontab command:

    Crontab-e

    Run this command and select the scheduler task editor. Generally, select nano (relatively simple)

    2. Enter the PHP code to be executed or other URL webpage:

    */3 *****/usr/bin/curl-o temp.txt http: // localhost/monitor. php

    The preceding command indicates that the monitor is executed every three minutes. the php code file is executed in minutes, hours, days, months, and years from the right to the right. You can also specify the time for execution:

    */3 9-18 ***/usr/bin/curl-o temp.txt http: // localhost/monitor. php

    The monitor. php code is executed every three minutes from AM to AM.

    3. After Entering the preceding command, press Ctrl + X and select Yes to save the scheduled task. A crontab is created successfully. To view the list of scheduled tasks of the current user, run the following command:

    Crontab-l



    Popularization of crontab knowledge


    Use crontab to regularly execute php Code, for example, once every 10 minutes:
    /10 ***** wget-q -- sqider http ://******
    1. Use PHP to execute scripts in Crontab
    Just like calling a common shell script in Crontab (specific Crontab usage), you can use a PHP program to call the PHP script.
    Execute myscript. php every hour as follows:

    The Code is as follows:


    # Crontab-e
    00 ***/usr/local/bin/php/home/john/myscript. php


    /Usr/local/bin/php is the path of the PHP program.
    2. Use the URL in Crontab to execute the script
    If your PHP script can be triggered through a URL, you can use lynx, curl, or wget to configure your Crontab.
    The following example uses the Lynx text browser to access the URL and execute the PHP script hourly. By default, the Lynx text browser opens a URL in dialog mode. However, as shown below, we use the-dump option in the lynx command line to convert URL output to standard output.

    The Code is as follows:


    00 ***** lynx-dump http: // www./myscript. php


    The following example uses CURL to access the URL and execute the PHP script every 5 minutes. Curl displays the output in standard output by default. With the "curl-o" option, you can also dump the Script output to a temporary file.

    The Code is as follows:


    */5 */usr/bin/curl-o temp.txt http: // www./myscript. php


    The following example uses the wget url to execute the PHP script every 10 minutes. -The q option indicates quiet mode ." -O temp.txt "indicates that the output will be sent to a temporary file.

    The Code is as follows:


    */10 */usr/bin/wget-q-O temp.txt http: // www./myscript. php


    Let's give a detailed introduction to him:
    Name: crontab
    Permission: All Users
    Usage:
    Crontab [-u user] file
    Crontab [-u user] {-l |-r |-e}
    Note:
    Crontab is used to allow users to execute programs at a fixed time or interval. In other words, it is similar to the user's time table. -U user is used to set the time table of the specified user. The premise is that you must have the permission (for example, root) to specify the time table of another user. If-u user is not used, the time table is set.
    Parameters:
    Crontab-e: Execute the text editor to set the time table. The preset text editor is VI. If you want to use another text editor, set the VISUAL environment variable to specify the Text Editor (for example, setenv VISUAL joe)
    Crontab-r: Delete the current time table
    Crontab-l: to list the current time table
    Crontab file [-u user]-replace the current crontab with the specified file.
    The time table format is as follows:
    F1 f2 f3 f4 f5 program
    F1 indicates the minute, f2 indicates the hour, f3 indicates the day of the month, f4 indicates the month, and f5 indicates the day of the week. Program indicates the program to be executed.
    When f1 is *, the program is executed every minute. When f2 is *, the program is executed every hour, and so on.
    When f1 is a-B, it indicates that execution is performed from the minute a to the minute B. When f2 is a-B, it indicates that execution is performed from the hour a to the hour B, and so on
    When f1 is */n, it indicates execution is performed every n minutes. If f2 is */n, it indicates execution is performed every n hours, and so on.
    When f1 is a, B, c ,... a, B, c ,... execute in minutes. f2 is a, B, c ,... a, B, c... execution in hours, and so on
    You can also store all settings in the file first, and use crontab file to set the time table.
    Example:
    # Run/bin/ls at every morning:
    0 7 ***/bin/ls
    During October 11, December, execute/usr/bin/backup every three hours from to every day:
    0 6-12/3*12 */usr/bin/backup
    From Monday to Friday, send a letter to alex@domain.name at pm:
    0 17 ** 1-5 mail-s "hi" alex@domain.name </tmp/maildata
    Execute echo "haha" at midnight, 00:20, and 02:20 every month"
    20 0-23/2 *** echo "haha"
    Note:
    When the program is executed at the specified time, the system will send you a letter showing the program execution content. If you do not want to receive such a letter, add>/dev/null 2> & 1 after each line is empty.
    Example 2:
    #06:10 every morning
    10 6 **** date
    # Every two hours
    0 */2 * date
    # Every two hours from PM to am, am
    0 23-7/2, 8 **** date
    # Am on the 4th day of each month and from Monday to Wednesday of each week
    0 11 4 * mon-wed date
    # A.m. of July
    0 4 1 jan * date
    Example
    $ Crontab-l list the current crontab of a user.
    The crontab command schedules the execution of some commands at certain intervals. There is a crontab file in the/etc directory, which stores some scheduling programs that are running systematically. Each user can create their own scheduling crontab.
    The crontab command has three forms of command line structure:
    Crontab [-u user] [file]
    Crontab [-u user] [-e |-l |-r]
    Crontab-l-u [-e |-l |-r] In the first command line, file is the name of the command file. If this file is specified in the command line, run the crontab command to copy the file to the crontabs directory. If this file is not specified in the command line, the crontab command will accept the commands typed on the standard input (keyboard) and store them in the crontab directory.

    In the command line, the-r option is used to delete the crontab file from the/usr/spool/cron/crontabs directory;
    In the command line, the-l option is used to display the contents of the crontab file.
    Run the crontab-u user-e command to edit the cron (c) job of the user. You can edit a file to add or modify any job requests.

    Run the crontab-u user-r command to delete all cron jobs of the current user.
    Jobs and their scheduled time are stored in the file/usr/spool/cron/crontabs/username. Username stores the user name in the corresponding file with the command that the user wants to run. The command execution result, whether it is standard output or error output, will be sent to the user in the form of mail. Each request in the file must contain six fields separated by spaces and tabs. The first five fields can take an integer to specify when to start working. The sixth field is a string called a command field, which includes the crontab command for scheduling and execution.
    The value range and significance of the integer in the first and fifth fields are as follows:
    0 ~ 59 indicates the score
    1 ~ 23 indicates the hour
    1 ~ 31 indicates the day
    1 ~ 12 indicates the month.
    0 ~ 6 indicates the week (0 indicates Sunday)
    /Usr/lib/cron. allow indicates who can use the crontab command. If it is an empty file, it indicates that no user can schedule a job. If this file does not exist and another file/usr/lib/cron. deny exists, the crontab command can be used only by users not included in this file. If it is an empty file, it indicates that any user can arrange jobs. If both files exist at the same time, cron. allow takes priority. If neither of them exists, only the Super User can schedule a job.


Related Article

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.