php exec() is not executing the command
I already said that I was new to exec()
function. After doing some more digging, I came upon 2>&1
which needs to be added at the end of command in exec()
.
Thanks @mattosmat
for pointing it out in the comments too. I did not try this at once because you said it is a Linux command, I am on Windows.
So, what I have discovered, the command is actually executing in the back-end. That is why I could not see it actually running, which I was expecting to happen.
For all of you, who had similar problem, my advise is to use that command. It will point out all the errors and also tell you info/details about execution.
exec('some_command 2>&1', $output);
print_r($output); // to see the response to your command
Thanks for all the help guys, I appreciate it ;)
PHP exec() command not working
Finaly found the solution!!
If you encounter the same problem as i did, and you dont know how to fix this, follow this steps.
- Login to the Remote of your server (if you have access to it)
- Open the IIS manager
- Select -> Application Pools node underneath the machine node (left panel)
- Right click on the desired domainname -> Advanced settings
- Scroll to Process Model -> Identity
- Click on the 3 dots
- Check the Custom Account radio button
- Fill in an existing administration account which has access to the
remote desktop and of course its password - Press OK
- Now go to the .exe or whatever file you want to access throught the
exec command and - Right click on the file and select properties
- Go to the security tab
- Select Edit
- Now grand full rights to the account you selected before. If its
not listed than add it manualy by clicking on Add
If you have done all of this, it now should work.
PHP exec() not working - permission error?
The major difference in such cases i find is the environment and the permissions. The error [4] => boost::filesystem::create_directory: Permission denied: "/.litecoin"
suggests that your php page is looking for the folder litecoin
in ~
or $HOME
and it is not set. That's why it becomes /.litecoin
I created a simple index.php
file with below code
<?php
$out = array();
exec("env", $out);
var_dump($out);
The output of same on my default php is
/home/vagrant/nginx/html/index.php:6:
array (size=9)
0 => string 'APACHE_RUN_DIR=/var/run/apache2' (length=31)
1 => string 'APACHE_PID_FILE=/var/run/apache2/apache2.pid' (length=44)
2 => string 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' (length=65)
3 => string 'APACHE_LOCK_DIR=/var/lock/apache2' (length=33)
4 => string 'LANG=C' (length=6)
5 => string 'APACHE_RUN_USER=www-data' (length=24)
6 => string 'APACHE_RUN_GROUP=www-data' (length=25)
7 => string 'APACHE_LOG_DIR=/var/log/apache2' (length=31)
8 => string 'PWD=/home/vagrant/nginx/html' (length=28)
As you can see there is $HOME
and that could be one possible reason causing a issue
So you should make sure the correct environment is available to the executable that you are running. You can do that by doing
exec("HOME=/var/www/html X=Y A=B env", $out);
The output of the command shows it can see the updated variables
/home/vagrant/nginx/html/index.php:6:
array (size=12)
0 => string 'HOME=/var/www/html' (length=18)
1 => string 'APACHE_RUN_DIR=/var/run/apache2' (length=31)
2 => string 'APACHE_PID_FILE=/var/run/apache2/apache2.pid' (length=44)
3 => string 'A=B' (length=3)
4 => string 'X=Y' (length=3)
5 => string 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' (length=65)
6 => string 'APACHE_LOCK_DIR=/var/lock/apache2' (length=33)
7 => string 'LANG=C' (length=6)
8 => string 'APACHE_RUN_USER=www-data' (length=24)
9 => string 'APACHE_RUN_GROUP=www-data' (length=25)
10 => string 'APACHE_LOG_DIR=/var/log/apache2' (length=31)
11 => string 'PWD=/home/vagrant/nginx/html' (length=28)
Figure out which environment variables you need and make sure they exists and try to set the paths so your executable used /var/www
or /var/www/html
whichever path it has access to and then executes the program
PHP not executing a shell exec command
Thanks to KIKO Software his link PHP quotes inside quotes. had the answer that I needed:
I had to escape the single quotes in the AWK
Working code:
<?php
$sinfo = shell_exec('ss -H -t -r state established | awk \'{$1=$2=""; print $0}\'');
echo "<pre>$sinfo</pre>";
?>
Related Topics
How to Enable Shell_Exec and Exec on PHP
Preparing PHP Application to Use with Utf-8
Location for Session Files in Apache/Php
Instagram Login Programmatically
Curl_Exec Printing Results When I Don't Want To
How to Start a Get/Post/Put/Delete Request and Judge Request Type in PHP
Object-Oriented-Like Structures in Relational Databases
Remove Duplicated Elements of Associative Array in PHP
Php_Network_Getaddresses: Getaddrinfo Failed: Name or Service Not Known
Symfony Redirect with 2 Parameters
Fatal Error: Cannot Use Object of Type Stdclass as Array In
How to Implement "Maintenance Mode" on Already Established Website
Recommended Server for Continuous Integration for a PHP Project
How to Detect and Handle MySQL Warnings with PHP
Troubleshooting "No Such File or Directory" When Running 'PHP App/Console Doctrine:Schema:Create'