Search code examples
phpunixcronexecpassthru

Getting response from unix commands executed via PHP


I would like to list the Cron tasks for a given user in the browser. I am using the following, which works fine via SSH:

crontab -u username -l

Which outputs something like:

*/2 * * * * cd /home/username/public_html/cron; php -q -c ./ cron_4.php
*/2 * * * * cd /home/username/public_html/cron; php -q -c ./ cron_3.php
*/2 * * * * cd /home/username/public_html/cron; php -q -c ./ cron_2.php
0 0 * * * cd /home/username/public_html/cron; php -q -c ./ cron_1.php

However, when I try to do it via PHP...

$return = array();
$command = "crontab -u username -l";
passthru($command, $return);
echo "<pre>";
var_dump($return);
echo "</pre>";

I only get...

int(1)

Whereas I was expecting an array containing each of the above lines.

How can I achieve the expected result via PHP?


Solution

  • I ended up using exec()

    $r=[];
    exec($command,$r);
    

    The actual problem was something to do with permissions and is irrelevant to the question as I stated it.