Php uses exec to execute shell scripts

Source: Internet
Author: User
Php uses exec to execute shell scripts. let's talk about what I want to do first:
Web-based operations in Linux allow you to perform basic operations on the web end, but now you are writing the code to create a directory:
$ Mkdir_command = "/home/myname/myshell/mkdir. sh/$ directory/$ name ";
Exec ($ mkdir_command, $ output, $ return );
.....
I also know that executing shell scripts on the web end is actually running with Apache permissions in Linux,
However, in Linux. sh has changed its permission to root at runtime. that is to say, any user who calls the file executes chmod 777 mkdir with the root permission. sh; chmod u + s mkdir. sh
However, the execution on the webpage is still unsuccessful.
In Linux alone, the execution is successful,
I hope you can express your comments or comments. thank you !!


Reply to discussion (solution)

Apache requires write permission on this directory

Apache requires write permission on this directory


Thank you for your reply. can you tell me how to set it?

Mkdir. sh permission changed to root
Apache has no access permission.

At the end of this post, xuzuning changed the mkdir. sh permission to root at 19:32:59 on.
Apache has no access permission.


Amount... Mkdir. sh is created by the root user, but the root permission is granted to the common user when the file is executed.
It is a great honor for the moderator to reply... Do you have any good methods?

1. the web writes the results to a file. for example, you can create a file based on what you want to create.

2. linux root regularly calls shell to check whether there is any file that needs to be updated.
In this case, the root user is used to perform the operation.


Just create a timer.

Do you have the execution permission for your php script? It's not mkdir. sh. Is it the php that runs shell? is the execution permission set?

1. the web writes the results to a file. for example, you can create a file based on what you want to create.

2. linux root regularly calls shell to check whether there is any file that needs to be updated.
In this case, the root user is used to perform the operation.


Just create a timer.


What do you mean by using the Linux crond service? Execute the file written by php at intervals and then delete it? Is this a good solution?

Do you have the execution permission for your php script? It's not mkdir. sh. Is it the php that runs shell? is the execution permission set?


Php operation permissions are certainly available.

You have to check whether you have the execution permission.
System/exec first executes a simple command such as ls and ifconfig to check whether the execution is successful.
Check the permission of the. sh file again.

In linux, enter do
Then we append the object at the end.
Apache ALL = NOPASSWD:/bin/sh
Then
Execute exec ("/usr/bin/sudo/bin/sh/xxx. sh") in php ")
/Xxx. sh is the script you want to execute

Let's take a look at this article to solve your problem.
Http://blog.csdn.net/vindo/article/details/3610587

Add sudo

In linux, enter do
Then we append the object at the end.
Apache ALL = NOPASSWD:/bin/sh
Then
Execute exec ("/usr/bin/sudo/bin/sh/xxx. sh") in php ")
/Xxx. sh is the script you want to execute

Let's take a look at this article to solve your problem.
Http://blog.csdn.net/vindo/article/details/3610587 ......


Thank you for solving the problem. Thank you!

Thank you for your answers. let's take a look and solve the problem! Hey!

How can I solve this problem? My script is used to obtain the virtual machine ip address, and cannot be executed now

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.