51

Is there a way to check in a php script if exec() is enabled or disabled on a server?

Adrian M.
  • 7,183
  • 15
  • 46
  • 54

10 Answers10

68

This will check if the function actually works (permissions, rights, etc):

if(@exec('echo EXEC') == 'EXEC'){
    echo 'exec works';
}
Tobias
  • 4,523
  • 2
  • 20
  • 40
Michael D Price
  • 681
  • 1
  • 5
  • 2
42
if(function_exists('exec')) {
    echo "exec is enabled";
}
nc3b
  • 15,562
  • 5
  • 51
  • 63
  • 37
    This is incorrect - it will only detect whether the function is available. Most servers will disable it via INI, which means the function will still be present but throw a warning when run and not do anything. See this answer: http://stackoverflow.com/questions/3938120/check-if-exec-is-disabled – pospi Aug 02 '12 at 02:31
  • 5
    @pospi That's nonsense; `function_exists()` also checks whether a function is disabled - [proof](http://lxr.php.net/xref/PHP_5_4/Zend/zend_builtin_functions.c#1349) – Ja͢ck Dec 08 '14 at 10:22
  • @Jack is that a statement you'd be comfortable making across all possible combinations of PHP versions and server APIs? (; – pospi Dec 10 '14 at 07:41
  • 1
    @pospi You mean for versions of PHP that aren't EOL? Sure. – Ja͢ck Dec 10 '14 at 07:46
  • @Jack hah, that's fair. But we don't all have the luxury of maintaining sites developed this side of 2003! I would say it's a gotcha to be aware of if you're working on pre-5.3 code but not something to worry about otherwise. – pospi Dec 17 '14 at 00:56
  • @pospi's comment is correct . There are cases where the PHP check in function_exists does not properly verify that the function is not present in the disabled list, and additional checking is required. – Scott C Wilson Oct 04 '17 at 13:21
9

ini_get('disable_functions')

What you actually want to do is use ini_get('disable_functions') to find out if it is available to you:

<?php
function exec_enabled() {
    $disabled = explode(',', ini_get('disable_functions'));
    return !in_array('exec', $disabled);
}
?>

Answered on stackoverflow here: Check if "exec" is disabled, Which actually seems to come from the PHP Man page: http://php.net/manual/en/function.exec.php#97187

Path

If the above returns true (you can use exec()), but PHP can still not trigger the script there is a good chance that you have a path issue for that script, test this by doing:

print exec('which bash');

and then try

print exec('which ogr2ogr');
Community
  • 1
  • 1
Duncanmoo
  • 3,535
  • 2
  • 31
  • 32
6

This will check that exec is available and enabled BEFORE trying to run it. If you run exec() and the function does not exist or is disabled a warning will be generated. Depending on the server settings that may render to the browser and will almost-always write a line to a log file = performance hit.

// Exec function exists.
// Exec is not disabled.
// Safe Mode is not on.
$exec_enabled =
   function_exists('exec') &&
   !in_array('exec', array_map('trim', explode(', ', ini_get('disable_functions')))) &&
   strtolower(ini_get('safe_mode')) != 1
;


if($exec_enabled) { exec('blah'); }
Community
  • 1
  • 1
Lance Cleveland
  • 3,098
  • 1
  • 33
  • 36
4

It's a bit tricky to find exec function available until unless checking all possibilities

1.function_exist('exec')

2.Scanning through ini_get('disabled_functions)

3.Checking safe_mode enabled

function is_shell_exec_available() {
    if (in_array(strtolower(ini_get('safe_mode')), array('on', '1'), true) || (!function_exists('exec'))) {
        return false;
    }
    $disabled_functions = explode(',', ini_get('disable_functions'));
    $exec_enabled = !in_array('exec', $disabled_functions);
    return ($exec_enabled) ? true : false;
}

This function never throws warnings unless ini_get function not disabled.

Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
1

I am assuming that you are running this on a linux server.

You could test the exec function by running the following php script:

exec("whoami", $ret);

echo $ret[0];

This will return the command whoami.

If it errors out, it is because the exec function could not run.

Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
tungpksa
  • 21
  • 1
1

Example:

if(strpos(ini_get('disable_functions'),'ini_set')===false) 
    @ini_set('display_errors',0);
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
Stergios Zg.
  • 652
  • 6
  • 9
0

This is some ugly code I made to detect if a function is enabled or not.

function is_enabled($f)
{
    if($f=='ini_get')return@ini_get('a')===false;
    return(($l=@ini_get('disable_functions'))===null||!is_callable($f)||!function_exists($f)||!in_array($f,array_map('trim',explode(',',$l)));
}

//Usage example:
print_r(is_enabled('str_split'));//true or null if ini_get() is disabled
Umbrella
  • 4,733
  • 2
  • 22
  • 31
Ismael Miguel
  • 4,185
  • 1
  • 31
  • 42
0

(Based on other responses) To check if exec exists and services are running:

function isRunning($serviceName)
{
    return exec('pgrep '.$serviceName);
}

if (@exec('echo EXEC') == 'EXEC') {
    $services = [
        'mysql',
        'nginx',
        'redis',
        'supervisord',
    ];

    foreach ($services as $service) {
        if (isRunning($service)) {
            echo $service.' service is running.<br>';
        } else {
            echo $service.' service is down.<br>';
        }
    }
}

// mysql service is running.
// nginx service is running.
// redis service is running.
// supervisord service is down.
Sinan Eldem
  • 5,564
  • 3
  • 36
  • 37
0

I would use this:

if (in_array('exec', preg_split('/\s*,\s*/', ini_get('disable_functions')) {
  echo "exec is disabled";
}
tim
  • 2,530
  • 3
  • 26
  • 45