Executing shell commands


Just a quick post - if you’re using PHP’s shell_exec to execute shell commands, take note of the following:

Permissions
If you’re reading or writing files, ensure that your working directory has the correct permissions for the www/apache user to access what it needs. On some systems, PHP will run as the nobody user, so adjust accordingly.

Pipes
Using pipes and redirection inside your command string is not going to work most of the time, what you probably want to do is create a seperate bash script on your server that contains all the commands you want to run. Then use PHP to execute the single bash script.

Paths
This one has got me a few times - just because a command is in the path when you’re SSH’d into the server, it doesn’t mean that it’ll be on PHP’s perceived path. This means that you’ll need to specify the full path to any system applications you are using (not for standard commands like cat, though). For example:

   shell_exec("/usr/local/bin/htmldoc --jpeg=0 --webpage \\
      -f out.pdf in.html");
Share this post
  • Digg
  • StumbleUpon
  • Reddit
  • del.icio.us
  • Facebook
  • muti
  • Mixx
  • Google
  • laaik.it

This entry was posted on Tuesday, March 6th, 2007 at 11:54 am and is filed under MySQL. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply