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
e开发者_如何学编程cho "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.
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
dpkg --info
doesn't require root permission while dpkg -i
does. Since you run your script on browser, this mean that your script run on php / apache's user. I believe, on most installation, that user doesn't have root permission.
To overcome this, you could create some sort of processor that will have the following step:
create queue
table:
- queue_id
- timestamp
- deb_file
- is_processed
browser script:
- upon clicking on install, insert an entry to queue table.
processor script (let's say queue_processor.php
):
- upon run, check if there are entries on queue table whose is_processed = 0
- if there are any:
- update is_processed = 1
- process that file
- after finished processing (installing deb file), set is_processed = 2
The final step is, to set queue_processor.php
on crontab and have it owner as root:
* * * * * root /path/to/php/binary/php /path/to/your/queue_processor.php
精彩评论