_php tutorials for executing PHP scripts with crontab timed under Linux

Source: Internet
Author: User
Tags parse error stdin
Recently, this method is urgently needed to record and try ^ ^ when you are free

Under Linux, you can use the Crontab + PHP method:

1. Use Crontab–e to edit timed tasks

The contents are:

Xx:xx:xx executing a test.php file

2, PHP files must be in the file header line, plus the interpreter path (as Perl did)

#!/usr/local/bin/php

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

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

Using PHP as the Shell scripting Language (reproduced)

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

Probably a lot of people want to use PHP to write some of the timing of the letter, such as the program, but there is no way to execute PHP timed, once to Phpbuilder, found this article, so I want to give you a translation (at the same time do some changes), I hope it is useful to everyone.

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

As we all know, PHP is a very good dynamic web development language (fast speed, short development cycle ...). But only a very small number of people realize that PHP can also be very good as the language of writing shell scripts, when PHP as the language of writing shell scripts, he is not as powerful as Perl or bash, but he has a good advantage, Especially for those of me who are familiar with PHP but are not familiar with Perl.

To use PHP as the Shell scripting language, you have to compile PHP as a binary CGI instead of Apache, and there are some security issues with PHP compiled into binary CGI mode, which can be found in the PHP Manual (http://www.php.net).

You might not get used to writing shell scripts at first, but it will get better: the only difference between PHP as a generic Dynamic Web page and as a Shell scripting language is that a shell script needs to explain the script's program path in the first line of life:

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

We add the parameter "1" After the PHP execution file, so that the child PHP will not output httpheader (if you still need to be a Dynamic Web page, then you need to use the header function output Httpheader). Of course, in the shell script you still need to use PHP's start and end tags:

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

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

Print ("Hello, world!");

?>

The above program will simply output "Hello, world!" to the monitor.

First, pass the shell script run parameters to PHP:

As a shell script, often add some parameters when running the program, PHP as a shell script with an inline array "$argv", using the "$ARGV" array can easily read the shell script Runtime parameters ("$ARGV [1]" Corresponds to the first argument, "$ARGV [2]" corresponds to the second parameter, 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 is you today? ", $first _name, $last _name);

?>

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

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

The shell script will output on the display:

Hello, Darrell brogdon! How is you today?

[Dbrogdon@artemis dbrogdon]$

PHP also contains the "$ARGV" array when it is written as a dynamic Web page, but there are some differences: when PHP is the Shell scripting language, "$argv [0]" corresponds to the file name of the script, and when used for dynamic Web page writing, "$argv [1]" Corresponds to the first parameter of the QueryString.

Second, write an interactive shell script:

If a shell script just runs on its own and loses interactivity, then it doesn't mean anything. When PHP is used for shell script writing, how to read the user input information? Unfortunately, PHP itself does not have a function or method to read user input information, but we can follow other languages to write a function that reads user input information "read":

function Read () {

$fp = fopen (/dev/stdin, R);

$input = fgets ($fp, 255);

Fclose ($FP);

return $input;

}

?>

It is important to note that the above function can only be used for UNIX systems (other systems need to be changed accordingly). The above function opens a file pointer and then reads a line of no more than 255 bytes (that is, fgets), then closes the file pointer and returns the read information.

Now we can use the function "read" To modify the program we wrote earlier in 1 to make him 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's your last name?");

$last _name = read ();

Print ("Hello, $first _name $last _name! Nice to meet you! ");

?>

Save the above program, run it, you may see an unexpected thing: the last line of input into three rows! This is because the "read" function returns the information also includes the end of the user each line of the line break "", to leave the last name and first name, to remove the end of the line break, you need to "read" function to modify:

function Read () {

$fp = fopen (/dev/stdin, R);

$input = fgets ($fp, 255);

Fclose ($FP);

$input = Chop ($input); Remove trailing blanks

return $input;

}

?>

C. Shell scripts written in other languages include shell scripts written by PHP:

Sometimes we may need to include shell scripts written by PHP in shell scripts written in other languages. Actually very simple, here is a simple example:

#!/bin/bash

Echo this was the Bash section of the code.

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

Print ("This was the PHP section of the Code");

?>

Eof

In fact, it is called PHP to parse the following code, and then output; then try the following code:

#!/bin/bash

Echo this was the Bash section of the code.

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

$myVar = PHP;

Print ("This was the $myVar section of the Code");

?>

Eof

You can see that the only difference in code two times is the second time using a variable "$myVar", try running, PHP unexpectedly gave the error message: "Parse error:parse error in-on line 2"! This is because the variables in bash are also "$myVar", The Bash parser first replaced the variables, in order to solve this problem, you need to precede each PHP variable with "" escape character, then the code is modified as follows:

#!/bin/bash

Echo this was the Bash section of the code.

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

$myVar = PHP;

Print ("This was the $myVar section of the Code");

?>

http://www.bkjia.com/PHPjc/486117.html www.bkjia.com true http://www.bkjia.com/PHPjc/486117.html techarticle recently need this method, record, have time to try ^ ^ in Linux, you can use Crontab + PHP method: 1, the use of Crontab e Edit timed task content: Xx:xx:xx execution ...

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