How to execute shell commands via PHP

This post discusses how to execute shell commands via PHP. The ability to execute shell commands is a powerful feature and should be used carefully. As such, not all hosting providers will allow you to execute shell commands.

Did you know that you can disable PHP functions in the php.ini file using the disable_functions directive. A disabled function will return an error like this: "Fatal error: Uncaught Error: Call to undefined function shell_exec()".

The PHP functions to execute shell command is: shell_exec(),  exec() or system(). These functions are remarkably similar but have slight differences. Let's take a look.

shell_exec(): string

The shell_exec() function returns a string or NULL value. The returned string will contain the output of the command that you executed. The shell_exec() function will, however, return a NULL if an error has occurred. You can also expect a NULL value if the command produces no output.

Parameters:

  • $cmd - Expects a string value that holds the command that you want to execute.

Next, let's look at how you can use the shell_exec() function to return a directory listing of a Linux machine:

<?
//List the folders using ls
echo shell_exec("ls / -ltr");

Result:

total 2097248 drwxr-xr-x 2 root root 4096 Apr 26 19:07 opt drwxr-xr-x 2 root root 4096 Apr 26 19:07 media drwxr-xr-x 2 root root 4096 Apr 26 19:07 lib64 lrwxrwxrwx 1 root root ….

exec(string $cmd, array &$output = null, int &$resultCode = null): string|false

The exec() function returns the last line of the executed command as a string. This command can, however, also return FALSE if an error has occurred.

Parameters:

  • $cmd - Expects a string value that holds the command that you want to execute.
  • &$output - Is an optional parameter that expects an array variable. The exec() function will then update the variable with every line of output from the command. Trailing white space, such as \n, is not included in this array. Also, exec() will append to the end of the array if the array already contains items.
  • &$resultCode - Is an optional parameter that expect an integer variable. The exec() function will then update the variable with the exit status of the executed command.
The & symbol means that the variable's value will be updated when it changes inside the function. In other words, the variable is passed by reference.

Next, let's look at how you can use the exec() function to return memory information of a Linux machine:

<?php
$output=null;
$resultCode=null;
//get the memory info of a Ubuntu machine
exec('free', $output, $resultCode);
echo "Returned with status $resultCode and output:\n";
print_r($output);

Result:

Returned with status 0 and output:
Array
(
    [0] =>               total        used        free      shared  buff/cache   available
    [1] => Mem:       16351256     9143564      283052      282788     6924640     6661072
    [2] => Swap:       2097148      279644     1817504
)

system(string $cmd, int &$resultCode = null): string|false

The system() function is similar toexec() function. It will however display output directly (without using echo() or print()).

Parameters:

  • $cmd - Expects a string value that holds the command that you want to execute.
  • &$resultCode - Is an optional parameter that expect an integer variable. The exec() function will then update the variable with the exit status of the executed command.

Finally, let's see how the system command is used using the exec() example from above:

<?php
$resultCode=null;
//get the memory info of a Ubuntu machine
system('free', $resultCode);
echo "Returned with status $resultCode and output:\n";

Result:

              total        used        free      shared  buff/cache   available
Mem:       16351256     9246984      238044      307576     6866228     6531868
Swap:       2097148      285036     1812112
Returned with status 0 and output:

Wrapping up

As you can see, it is easy to execute shell commands in PHP. Also, the shell_exec(),  exec(), or system() function must not be disabled in the php.ini file.

You may also be interested in

Updated:

  • 2021-07-27 - Added information about disabling functions in the php.in.

Source:



About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



2 Comments on “How to execute shell commands via PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *