【发布时间】:2019-01-04 05:18:59
【问题描述】:
脚本:
$S = 'pgrep -f test.php | wc -l';
$U = trim(shell_exec($S));
echo $U;
为什么这个脚本的结果等于0,有时等于1?
文件test.php 没有运行。
【问题讨论】:
-
因为 grepping 也是一个新进程,所以需要从结果中过滤 grep 进程。使用 ps aux 你可以:
ps aux | grep test.php | grep -v grep | wc -l -
@AgniusVasiliauskas 这也适用于其他命令,将结果(无论它可能是什么)通过管道输送到
grep -v pgrep是一个否定,它表示:“所有除了通过指定的内容-v标志”。
标签: php bash grep shell-exec