Search code examples
phpbashshellterminaldpkg

PHP running bash (.sh) script. dpkg --info works but not dpkg -i


Sample PHP Script looks like this:

#!/usr/bin/php
    $file = '/private/var/www/app/install.sh';
    if(is_file($file)){
        $output = shell_exec('bash /private/var/www/app/install.sh');
        fwrite(STDOUT, $output."\n");
    }
    exit(0);

install.sh Example

#!/bin/bash
clear
echo "Executing Install Script..."
dpkg --info /private/var/www/app/app.deb
dpkg -i /private/var/www/app/app.deb
echo "Script Finished"
exit 0

This will print out the resulting dpkg --info data, but it will not run dpkg -i. It doesnt print out any errors, or anything at all...

Also, this is being executed via a web web browser. When the script is run from the terminal, it works fine. But on the web, only the info command is returned.


Solution

  • There are probably some security concerns with what you are trying to do here, and I'm not going to comment on those. However, I'd guess that dpkg -i needs to run as root, and your webserver (which executes the install.sh script) is not running as root. On the other hand, the dpkg --info command doesn't need root privileges to run, and so you will see its output when executed via the webserver's user. If you really need to run this script as root, you might want to look at a specific /etc/sudoers config. Perhaps start here: https://help.ubuntu.com/community/Sudoers

    and take a look here: http://ubuntuforums.org/showthread.php?t=1132821