PHP MySQL Tutorial
Learn PHP and MySQL

Executing a Shell Command Using PHP

Page Details

Published by:
admin
on 12-20-2008
3 people found this article useful.

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: haoxxooo Posted on 06-05-2009 12:32 AM

yes, it's true

By: Ahmed Mohamed Zain Posted on 09-13-2009 2:27 AM

yes it's true !

Powered by Community Server (Non-Commercial Edition), by Telligent Systems