Why doesn't exec(“top”); work on Linux?

后端 未结 5 1193
醉酒成梦
醉酒成梦 2020-12-21 09:54

I was trying to execute this command

echo exec(\"top\");

and

echo exec(\"/usr/bin/top\");

neither works

5条回答
  •  不思量自难忘°
    2020-12-21 10:04

    It probably works, but exec() doesn't return anything. Read the Manual: exec()

    $output = null;
    exec('top', $output);
    echo $output;
    

    But you have another problem: top doesn't exit by itself. You cannot use it here, because you need to send the interrupt-signal (just realized: q is ok too).

    One solution is to make top to stop after one iteration

    $output = null;
    exec('top -n 1', $output);
    var_dump($output);
    

提交回复
热议问题