PHP: exec command not working; sh: command not found

When using PHP exec command, if you find sh: Command not found , error in your web server logs, you have to export the full path of that command.

Assuming, that command in /opt/local/bin, change the command to
 $cmd ='export PATH=$PATH:/opt/local/bin; myCommand 2>&1';  
 echo exec($cmd,$out);  

To debug PHP exec command, Read this

For example:
For the PHP code:
 $cmd = "gmake";  
 echo exec($cmd,$out);  

There will be no output. In your web server error log, your will have :
 sh: gmake: command not found  
To fix this, we need to export the PATH of the gmake.

Go to your terminal, find where your command is located, In this example:
 $ which gmake  
 /opt/local/bin/gmake  

So, modify you PHP code as following:
 $cmd ='export PATH=$PATH:/opt/local/bin; gmake 2>&1';  
 echo exec($cmd,$out) ;

This will output, on your web page
 gmake: *** No targets specified and no makefile found. Stop.  

6 comments:

  1. Hi Juan,
    Can u tell me the command string your trying to execute

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi Juan,

    I want to execute sh file in php. Below is my code .
    <?php
    $output = shell_exec("sh /home/abc.sh");
    echo "<pre>$output</pre>";

    ?>
    when i run on browser i am not getting any out put.

    So plz help me

    ReplyDelete
    Replies
    1. Can you by appending 2>&1 to the command...

      echo shell_exec("sh /home/abc.sh 2>&1");

      Delete
  4. can you give meyour fb id please ?

    ReplyDelete

Python contextlib for Timing Python code

If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...