I'm attempting to run the following command through PHP:

$output = shell_exec('sudo /Users/Derk/Code/automation/psc/pdfs.sh 1');

This runs the following bash script code:

sudo /usr/local/bin/pdftk cvcheck_template.pdf fill_form data.fdf output output.pdf flatten    
sudo /usr/local/bin/convert -density 150 -gravity south -geometry +0+150 -composite output.pdf signature.png new_output.pdf

The bash script works perfectly in the command line but when I attempt to run it via PHP on a web server (MAMP) it does not give me any output. The first command seems to work but the second command gives me nothing.

Any help would be much appreciated.

  • Why are you chaining sudo? First to run the script, and then the script apparently also runs sudo. Once is enough... Is the PHP being executed from the shell, or from a webserver? If the latter, check the webserver error log.
    – wurtel
    Oct 22 '15 at 14:43
  • The PHP is being run through a webserver (MAMP). I checked the PHP error log but it does not show anything unfortunately. Could it be a user permissions issue?
    – dazzatron
    Oct 22 '15 at 23:18
  • You could create a logfile somewhere, e.g. /tmp/debuglogfile and make it writeable by all: chmod 666 /tmp/debuglogfile. Then change the PHP code to shell_exec('sudo /Users/Derk/Code/automation/psc/pdfs.sh 1 2>/tmp/debuglogfile') and see if any error output is written to the logfile. As you write the first command is executed (make sure it is! "seems to work" doesn't cut it), then sudo is apparently set up correctly, so permissions shouldn't be the problem... does the user under which the webserver runs have the same sudo rights as you?
    – wurtel
    Oct 23 '15 at 8:01

It ultimately came down to a permissions issue. The individual files touched through running Python scripts through the web browser need to have write access in the www-data group.

My mistake was that they had read and execution rights but no writing rights. So once I chmodded the appropriate files everything worked beautifully.

I also used sudo -u www-data command to test the PHP files without having to test them through the browser. That made testing a lot faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.