How to use Crontab for Linux to execute PHP scripts on a regular basis

Updated on technology 2024-02-09
4 answers
  1. Anonymous users2024-02-05

    Let's start with the relatively complex server implementation of PHP. If you have PHP installed on your server, you can execute PHP files, regardless of whether you have installed server environment software such as Nginx or Apache. In Linux, using the command line and crontab to schedule tasks is an excellent choice, and it is also the most efficient choice.

    First, enter the command line mode. Linux as a server generally enters the command line mode by default, of course, our management server is also generally connected to the server remotely through tools such as putty, for convenience, we log in with the root user. On the command line, type:

    crontab -e

    After that, a file will be opened, and it is a non-state, it is the interface of VI, by hitting the i on the keyboard, enter the mode, and you can have the content. Each line in this file is a scheduled task, and when we create a new line, we create a new scheduled task (of course, it means that this line is written according to a certain format). Let's take an example and add a line that reads as follows:

    What does this mean? In fact, the above line consists of two parts, the first part is time, and the second part is the operation content. For example, the one above, 00*

    When the number of minutes in the current time is 00, the scheduled task is executed. The time part consists of 5 time parameters, which are:

    Tick day, month, week.

    Column 1 represents minute 1 59 per minute is represented by or *1, n means per n minutes, for example * 8 means every 8 minutes, and the following is also an analogy.

    Column 2 represents hour 1 23 (0 for 0 o'clock).

    Column 3 represents date 1 31

    Column 4 represents the month 1 12

    Column 5 identifies day 0 6 (0 for Sunday).

    The rest of the sentence is the specifics of the operation.

    This means that the URL is accessed through Lynx. We mainly use Lynx, Curl, and Wget to achieve remote access to URLs, and if you want to improve efficiency, it is best to use PHP to execute local PHP files directly, such as:

    This statement can be executed every 2 hours and 0 minutes, through the internal PHP environment of Linux, note, this is not accessed through the URL, executed through the server environment, but directly executed, because the server environment is bypassed, so of course the efficiency is much higher.

    Okay, we've added a few required scheduled tasks. Click the esc key on the keyboard and enter ":wq", so that the set scheduled task will be saved, and you can also see a prompt on the screen to create a new scheduled task. The next thing is to write yours well.

    I won't introduce more usage of crontab here, if you want to use this scheduled task function more flexibly, you should learn crontab in depth by yourself.

  2. Anonymous users2024-02-04

    The two methods of crontab are described below.

    1. Use php to execute scripts in crontab.

    Just like invoking a normal shell script in crontab (specific crontab usage), use a php program to invoke php scripts.

    Each hour is performed as follows:

    **As follows: crontab -e

    usr local bin php is the path of the php program.

    2. Use url to execute scripts in crontab.

    If your PHP script can be triggered by a URL, you can use Lynx or curl or wget to configure your crontab.

    The following example is using a Lynx text browser to access a URL to execute PHP scripts on an hourly basis. By default, the Lynx text browser uses a conversational approach to open URLs. However, like the one below, we use the -dump option in the lynx command line to convert the output of the URL to standard output.

    ** As follows: The following example is to use the curl access URL to execute a php script every 5 minutes. By default, curl displays the output in standard output. Using the "curl -o" option, you can also dump the output of the script to a temporary file.

    ** As follows: The following example is using a wget access URL to execute a php script every 10 minutes. The -q option indicates quiet mode. -o indicates that the output is sent to a temporary file.

    ** Below:

  3. Anonymous users2024-02-03

    1: Use crontab -e to schedule commands, and use curl to run php page scripts.

    For example, the first 01 indicates that it is executed once in the first minute of each hour. If there are 5 *s, it means that the execution is performed every minute.

  4. Anonymous users2024-02-02

    In Linux, crontab is a command that implements scheduled execution, and we can use this instruction to execute a PHP file at a regular time, so as to realize the automatic execution of Linux scripts with PHP.

    How to use php as a Linux auto-execute script?

    When installing PHP, an executable file will be generated with the file name PHPCopy it to usr local bin. Execute PHP programs in terminal mode:

    php -q

    PHP is originally intended to be used in web applications, so it sends HTML headers, but here we are going to use PHP as shell script"-q"That means don't send headers. You can try to see the results without -q. At this point, you can run PHP ** in terminal mode.

    Linux command: cron daemon

    This is a system-based service that performs routine tasks such as checking disks once a day or once a month. Cron Daemon checks the scheduled worksheet every minute to see if there are any instructions to be executed, and all the output is emailed to the user.

    Set the crontab

    Command: crontab -e

    The command calls the viper to execute the manifest. For example.

    0 0 1,15 * fsck /home

    1 * /home/myhome/printhello

    Each row represents a scheduled job, preceded by the scheduled time, with a total of 5 fields, spaced apart, in the following order from left to right:

    minutes from 00 to 99

    O'clock from 0 to 24

    Day from 01 to 31

    month from 01 to 12

    Day 01 to 07, representing Monday to Sunday.

    number"each"If it is performed every day, fill in the * sign in the third column.

    So the two jobs in the above example are:

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

    The first minute of each hour executes the home myhome printhello file.

    Look at crontab: crontab -l

    Delete crontab: crontab -r

    We know how to use php and crontab commands for Linux auto-executing scripts.

    What else do I need to pay attention to?

    If you use a database in PHP, such as Oracle, you need to set the environment for exporting Oracle in the execution script. Because the script execution under cron is executed by the user without logging in, there are many less values of environment variables set in the system, and there will be problems when connecting to the database.

Related questions
3 answers2024-02-09

If the foundation is good, you can read the book directly and follow the operation. >>>More

4 answers2024-02-09

tar jxvf and then enter the directory that has just been extracted, and execute the following command. >>>More

9 answers2024-02-09

Detect the Linux system.

The following describes whether to enable the FTP server >>>More

9 answers2024-02-09

This problem is not operated in linux, you delete linux, how to use your machine without a system? >>>More

17 answers2024-02-09

The landlord actually wants to do foreign trade, it must be risky, there is indeed the kind of situation that the landlord said does not pay for the goods, but after all, it is a minority, can not because of the risk of a small probability, it denies the mainstream mode of operation, besides, PayPal is not biased in the transaction, if you send the goods, the customer does not pay, you can dispute with PayPal, show PayPal proof that you did ship the goods, as long as PayPal determines that the situation is true, will force the customer to payOn the other hand, because the popularity of PayPal abroad is really high, so if you refuse the payment method of PayPal, it is equivalent to rejecting a large number of potential customers, and there are customers to have business, so the landlord thinks twice, if the landlord wants to consult any questions about PayPal, you can add me msn:luke