ollazarev
ollazarev

Reputation: 1096

don't return the output after executing command via shell

After execution of php script in Fedora 20:

echo shell_exec('which systemctl');

empty string is displayed.

If to execute 'which systemctl' in command line, the following is showed:

/usr/bin/which: no systemctl in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

How to me to receive this output by means of php?

Upvotes: 3

Views: 892

Answers (2)

Ivan
Ivan

Reputation: 714

I'm using passthru in my project like the code below:

$output = '';
ob_start();
passthru('which systemctl', $output);
$output = ob_get_contents();
ob_end_clean();

And shell_exec also return an output please see the doc here: http://php.net/manual/en/function.shell-exec.php

But either one passthru and shell_exec is working.

Upvotes: 0

mhawke
mhawke

Reputation: 87074

Because systemctl is not on your path (or not on you system) which systemctl returns the error message

/usr/bin/which: no systemctl in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

When you run it in PHP using shell_exec('which systemctl'); the standard error is not collected, and the standard out is empty. Hence PHP sees an empty string.

You can get standard error using this command:

shell_exec('which systemctl 2>&1');

Upvotes: 3

Related Questions