PHP MySQL Tutorial
Learn PHP and MySQL

Executing a Shell Command Using PHP

100% of people found this useful
Executing a Shell Command Using PHP

You can use either system() or exec(). Just remember to ALWAYS use the full path to the command. So instead of entering 'top -n1' use '/usr/bin/top -n1'. So may also need to provide additional paramater to the command so it will run correctly as a background process. Using 'top' command as example again you need to add the '-b' parameter to make it run correctly like this '/usr/bin/top -b -n1'

When you use system() it will print all the command output to the browser. If you simply want to execute a command and you don't want the result to be displayed just redirect the output to some file like this :

system('/usr/bin/top -b -n1 > /dev/null 2>&1')

If you want to get the output (for further processing etc) use exec(). For example :

exec('/usr/bin/top -b -n1', $result)

$result will become an array containing every line of output from the 'top' command.

Recent Comments

By: john9 Posted on 03-27-2015 7:10 AM
By: john9 Posted on 04-09-2015 10:07 AM
By: john9 Posted on 04-09-2015 10:12 AM
By: john9 Posted on 04-13-2015 4:03 AM
Powered by Community Server (Non-Commercial Edition), by Telligent Systems