Execute PHP script file directly under Unix/linux

Source: Internet
Author: User
Tags php script

Since our servers are all Linux systems and have a complete PHP environment, I sometimes use PHP to write scripts to automate tasks, but every time I execute this php script I need to use PHP myscript.php, a little bit? Long F 涫 bear the strong shoot of benzene and onion PHP script files.

Write your script file

Here we write a file with a name of test_run.php, and the contents of the document are as follows:


This is some plain text.
This is the file name:
<?php
echo $argv [0], Php_eol;
?>


The script content is simple, which is to print out the name of the current script file.

We then use the PHP command to execute this script:


Yuanyu@ymac:phpworkspace $ php test_run.php Hello
This is some plain text.
This is the file name:
test_run.php
Yuanyu@ymac:phpworkspace $

Add header information to the script file and set permissions

Then, on the first line of the file, write the full path to the PHP command, preceded by a #!:


#!/usr/bin/php
This is some plain text.
This is the file name:
<?php
echo $argv [0], Php_eol;
?>


The file is then given the executable permission:


Yuanyu@ymac:phpworkspace $ chmod u+x./test_run.php

You can then execute the script directly:


Yuanyu@ymac:phpworkspace $./test_run.php
This is some plain text.
This is the file name:
./test_run.php
Yuanyu@ymac:phpworkspace $

This approach is also stated in the PHP official documentation, please refer to:

http://php.net/manual/en/features.commandline.usage.php

In the document

"Example #2 Script intended to is run from command line (script.php)"

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.