0

I'm trying to get information on the "updateable" packages on Linux (as given in "apt-get upgrade") via PHP.

Basically the information I'm interested in is the output of "apt-get upgrade" - especially the line: "29 upgraded, 0 newly installed, 0 to remove and 0 not upgraded." that appears even if you don't proceed with the updates.

Therefore I tried to run

print_r(exec('apt-get upgrade'));

but this has no output in PHP. Is there any way to force exec to "wait" for the output? Or is "apt-get" locked for exec() in general?

Thanks for your help

Daniel K.
  • 1,169
  • 1
  • 10
  • 25

1 Answers1

2
  1. Is safe_mode disabled?
  2. Use shell_exec() instead of exec()
  3. Check permissions for the user running the script (it might be necessary to run apt-get upgrade as root / sudo)
  4. try this:

$pkgs = shell_exec('apt-get upgrade'); echo "<pre>$pkgs</pre>";

Explanation:

exec() only returns the last line of the output while shell_exec() outputs the whole stream.

VF_
  • 2,509
  • 15
  • 17
  • safe_mode is off by default (double checked it right now) and I tried your snippet but it didn't change - blank page – Daniel K. Apr 24 '14 at 15:41
  • well I'm pretty sure it's a permissions issue then.. have you tried running the snippet in the php cli as root, because I just did and it worked flawlessly! – VF_ Apr 24 '14 at 15:48
  • Well I already thought that it might be an permission issue because it's kind of a security problem to enable package installations in simple php scripts. Any idea how I can fix this permission issue so that I can run the command directly in the php file? – Daniel K. Apr 24 '14 at 23:22
  • check out this post and the answer marked as correct.. I think it's by far the best solution: http://stackoverflow.com/questions/8532304/execute-root-commands-via-php – VF_ Apr 25 '14 at 09:20