How to execute shell script using php?
Try this. It issues the command in the same command as the login as I mentioned in my comment above:
$output1 = shell_exec('service asterisk_ipu_gui start');
$output2 = shell_exec('asterisk -vvvr "manager show connected"');
$output2
should hold your expected results now.
Also, try to add your www-data user to asterisk group, this may be due to permissions and ownership issues. If you can do it locally with 127.0.0.1, that means your local user is allowed to do it and it works, but when php tried via www-data, it fails.
Executing a shell script from a PHP script
I would have a directory somewhere called scripts under the WWW folder so that it's not reachable from the web but is reachable by PHP.
e.g. /var/www/scripts/testscript
Make sure the user/group for your testscript
is the same as your webfiles. For instance if your client.php
is owned by apache:apache
, change the bash script to the same user/group using chown
. You can find out what your client.php
and web files are owned by doing ls -al
.
Then run
<?php
$message=shell_exec("/var/www/scripts/testscript 2>&1");
print_r($message);
?>
EDIT:
If you really want to run a file as root from a webserver you can try this binary wrapper below. Check out this solution for the same thing you want to do.
Execute root commands via PHP
How to execute shell from php script
Depending on if you need the output of the script there are different approaches.
exec
executes a command and return output to the caller.passthru
function should be used in place of exec when the output from the Unix command is binary data which needs to be passed directly back to the browser.system
executes an external program and displays the output, but only the last line.popen
— creates a new process that is unidirectional read/writableproc_open
— creates a new process that supports bi-directional read/writable
For your scrapy script I would use a combination of popen
and pclose
as I don't think you need the script output.
pclose(popen("scrapy crawl example -a siteid=$id > /dev/null &", 'r'));
Can't execute shell script from php. Returns code 126
From https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
If a command is found but is not executable, the return status is 126.
Maybe you did have granted the permission to execute your script, but the user executing it simply do not have permission to execute bash, or even echo
If you don't have the mean to check your bash location or permissions, you can try to remove the shebang line (#!/bin/bash
), it's not very clean but most of the time it's not needed if your script is simple and can be executed with any shell
Run bash script from PHP script
I found what the issue was. It was because of Linux SELinux feature. This feature applies a least-privilege policy and denies any unnecessary command from running on Linux. The bash script is running successfully after disabling this feature. To do so, edit the file /etc/selinux/config and change SELINUX=enforcing to SELINUX=disabled and reboot the system. THIS IS NOT RECOMMENDED FOR SECURITY REASONS, however. You may check the link below to only create some exceptions rather than completely disabling SELinux.
https://wiki.centos.org/HowTos/SELinux
Related Topics
How to Use Laravel Passport with Password Grant Tokens
How to Get PHP, Symlinks and _File_ to Work Together Nicely
Format Output of $Simplexml->Asxml();
Handling Multiple File Uploads in Sonata Admin Bundle
Navigate Back with PHP Form Submission
Error Checking for Pdo Prepared Statements
What Does the Percent Sign Mean in PHP
Allow Users to Download Files Outside Webroot
Regex - How to Insert String Before File Extension
Long Running Background PHP Script Blocks Other PHP Pages Until It Is Finished
Native PHP Support in Visual Studio 2010
How to Parse Soap Response Without Soapclient
How to Measure the Strength of a Password
Generate Download File Link in PHP
Php/Gd - Transparent Background
Using Curl in PHP with Client Certificate and Private Key in Separate Files
How to Check If a Longitude/Latitude Point Is Within a Range of Coordinates