Use crontab regularly in the Linux PHP script

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags apache array code crontab echo example file function

This method is urgently needed, record, try ^^ when available

Under linux, you can use the crontab + php method:

1, use crontab-e edit scheduled tasks

Content is:

xx: xx: xx Execute a test.php file

2, php file must be in the first line of the file, add the interpreter path (as perl do)

#! / usr / local / bin / php

PHP implementation requires Apache support, shell script execution requires Linux support, and Linux supports the ability to run a program on a regular basis

-------------------------------------------------- -------------

Use PHP as a shell scripting language (Reprinted)

- Original in English: Darrell Brogdon, published at http://www.phpbuilder.com/columns/darrell20000319.php3)

Many people may have thought of using PHP to write some regular letters and the like program, but there is no way to time-based implementation of PHP; PHPBuilder time to go and found this article, so I want to give you a translation (and made some changes ),hope its good for U.S.

-------------------------------------------------- --------------------------------

We all know that PHP is a very good dynamic web development language (fast, short development cycle ...). But only a handful of people realize that PHP works well as a language for writing shell scripts. When PHP is the language for writing shell scripts, he is not as powerful as Perl or Bash, but he has great advantages, Especially for people like me who are familiar with PHP but are not familiar with Perl.

To use PHP as a shell scripting language, you have to compile PHP as a binary CGI rather than Apache. Compiling PHP as a binary CGI mode has some security issues. For a solution, see the PHP manual /www.php.net).

You may not be comfortable with writing shell scripts at first, but it will get better: The only difference between using PHP as a general dynamic web authoring language and as a shell scripting language is that a shell script needs to be interpreted in the first line of life The script's program path:

#! / usr / local / bin / php -q

We added the parameter "-1" to the PHP executable so that PHP will not output the HTTPHeader (you will need to output the HTTPHeader yourself using the header function if you still need to use it as a dynamic Web page). Of course, you still need to use the PHP start and end tags in the shell script:

Now let us look at an example to better understand the use of PHP as a shell scripting language:

#! / usr / local / bin / php -q

print ("Hello, world! n");

?>

The above program will simply output "Hello, world!" To the display.

First, pass the Shell script operating parameters to PHP:

As a shell script, we often add some parameters when we run the program. PHP has an embedded array "$ argv" as a shell script. Using the "$ argv" array, it is easy to read the parameters of the shell script when it runs "$ Argv [1]" corresponds to the first argument, "$ argv [2]" corresponds to the second argument, and so on). For example, the following program:

#! / usr / local / bin / php -q

$ first_name = $ argv [1];

$ last_name = $ argv [2];

printf ("Hello,% s% s! How are you today? n", $ first_name, $ last_name);

?>

The above code at run time requires two parameters, namely, first and last names, such as this run:

[dbrogdon @ artemis dbrogdon] $ scriptname.ph Darrell Brogdon

Shell script on the monitor will be output:

Hello, Darrell Brogdon! How are you today?

[dbrogdon @ artemis dbrogdon] $

The "$ argv" array is also included in PHP as a dynamic web authoring language, but here's a bit different: "$ argv [0]" corresponds to the script's filename when PHP is the shell scripting language, and when When used for dynamic web authoring, "$ argv [1]" corresponds to the first parameter to QueryString.

Second, write an interactive shell script:

If a shell script just run on their own, lost interactivity, then it does not mean much. When PHP is used in the preparation of Shell script, how to read the information entered by the user? Unfortunately, PHP itself does not read the user input information function or method, but we can follow other languages ​​to write a read user input Function "read":

function read () {

$ fp = fopen ('/ dev / stdin', 'r');

$ input = fgets ($ fp, 255);

fclose ($ fp);

return $ input;

}

?>

Note that the above function can only be used on Unix systems (other systems need to be changed accordingly). The above function will open a file pointer, and then read a no more than 255 bytes (that is, the role of fgets), and then close the file pointer to return to read the information.

Now we can use the function "read" to modify the program we wrote earlier to make it more "interactive":

#! / usr / local / bin / php -q

function read () {

$ fp = fopen ('/ dev / stdin', 'r');

$ input = fgets ($ fp, 255);

fclose ($ fp);

return $ input;

}

print ("What is your first name?");

$ first_name = read ();

print ("What is your last name?");

$ last_name = read ();

print ("nHello, $ first_name $ last_name! Nice to meet you! n");

?>

Save the above program, run it, you may see something unexpected: the last line of input into three lines! This is because the "read" function returns the information also includes the user's each line At the end of linefeed "n", retained in the surname and name, to remove the end of the newline, you need to modify the "read" function:

function read () {

$ fp = fopen ('/ dev / stdin', 'r');

$ input = fgets ($ fp, 255);

fclose ($ fp);

$ input = chop ($ input); // Remove the trailing white space

return $ input;

}

?>

Third, Shell scripts written in other languages ​​include shell scripts written in PHP:

Sometimes we may need to include shell scripts written in PHP in shell scripts written in other languages. In fact, very simple, the following is a simple example:

#! / bin / bash

echo This is the Bash section of the code.

/ usr / local / bin / php -q << EOF

print ("This is the PHP section of the coden");

?>

EOF

In fact, call PHP to parse the following code, and then output; Then, try the following code:

#! / bin / bash

echo This is the Bash section of the code.

/ usr / local / bin / php -q << EOF

$ myVar = 'PHP';

print ("This is the $ myVar section of the coden");

?>

EOF

It can be seen that the only difference between the two code is the second use of a variable "$ myVar", try running, PHP even gave the wrong message: "Parse error: parse error in - on line 2"! Because variables in Bash are also "$ myVar", and the Bash parser takes the variable first, to get around this, you need to prefix each PHP variable with "" escapes, so just the code Modify as follows:

#! / bin / bash

echo This is the Bash section of the code.

/ usr / local / bin / php -q << EOF

$ myVar = 'PHP';

print ("This is the $ myVar section of the coden");

?>


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.