dmallia Posted September 2, 2012 Share Posted September 2, 2012 (edited) So i am building an admin page as part of a project for the course I am doing. I want to execute the remote command "top" (it doesn't matter if i execute it by php ssh connection) on linux and than I want to store the output of the command in a variable called $data. Is that possible? I tried doing it with shell_exec() and exec() but couldn't get it to work. Thanks before hand. Edited September 2, 2012 by dmallia Link to comment Share on other sites More sharing options...
es131245 Posted September 3, 2012 Share Posted September 3, 2012 What OS are you using?For FreeBSD use "top -d 1"For Linux use "top -n 1"cuz"top" needs a input for exit so its not a good idea to use just "top"but its will work perfect with "ps" command Link to comment Share on other sites More sharing options...
justsomeguy Posted September 4, 2012 Share Posted September 4, 2012 shell_exec is the function to use to execute a shell command and store the output. Note you can only run that command on the server that PHP is running on, if you want to execute it on a remote server then you need to use something like SSH to connect, log in, and then run commands. Link to comment Share on other sites More sharing options...
dmallia Posted September 7, 2012 Author Share Posted September 7, 2012 (edited) $data = shell_exec('top -d 1');echo $data; i tried this code but nothing is loading. than just to test if the code is good i tried this $data = shell_exec('free -m');echo $data; and it worked correctly Edited September 7, 2012 by dmallia Link to comment Share on other sites More sharing options...
justsomeguy Posted September 7, 2012 Share Posted September 7, 2012 If you're trying to capture output from top then you should run it in batch mode with a set number of iterations, probably only 1. Link to comment Share on other sites More sharing options...
dmallia Posted September 7, 2012 Author Share Posted September 7, 2012 $output = null;exec('top -n 1', $output);print_r($output); i modified the script but the array is empty. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 7, 2012 Share Posted September 7, 2012 You need to run top in batch mode, check the options for it. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now