views:

152

answers:

2

I am trying to execute a shell command via:

<php echo shell_execute("tac /home/kusmuk/access-logs/kusmuk.org"); ?>

But it does not give any output. What could be the reason?

Although it does not work, the following lines work as expected:

<php echo shell_execute("ls -al triogrup.com"); ?>
//outputs: -rw-r----- 2 root kusmuk 28640 Aug 19 17:44 kusmuk.org

<php echo shell_execute("pwd"); ?>
//outputs: /home/kusmuk/public_html
+2  A: 

Try this:

echo shell_exec("tac /home/kusmuk/access-logs/kusmuk.org 2>&1");

It will redirect stderr to stdout so hopefully you should see why it's not working

Greg
It may be something simple like 'tac' not being in the PATH.
Steve Madsen
+2  A: 

Greg's tip is good. You'll probably end up with some kind of permissions problem.

However, I would say it's a good idea to avoid launching system calls from PHP if possible. The debugging can be a pain and if you're passing parameters it is very easy to make security holes. Native PHP code is much easier to handle.

‘tac’ is simple enough that you should be able to do it fine from within PHP. For example a trival version that spits out the whole file in one go:

$log= file_get_contents('/home/kusmuk/access-logs/kusmuk.org');
echo implode("\n", array_reverse(explode("\n", $log)));
bobince