查看进程就是使用ps命令而已,只不顾ps的参数太多了。

  使用php查询的话,必须要开启几个函数(可以执行外部程序的函数),参考官网:http://php.net/manual/zh/book.exec.php

  下面是在php进程中查询某个服务创建的进程数,比如httpd,mysqld,sshd.......

<?php
function query_process_num($service){
$res = array();
exec("ps -ef | grep " . $service ." | wc -l", $res);
return trim($res[]) - ;
} echo query_process_num("httpd");
?>
  至于为什么要减2,可以看下面的代码: <?php
function query_process_num($service){
$res = array();
exec("ps -ef | grep " . $service, $res);
print_r($res);//不处理直接输出 unset($res);
exec("ps -ef | grep " . $service . " | wc -l", $res);
print_r($res);//统计输出 } query_process_num("httpd");
?>
  输出如下:
~/tmp/test $ ps -ef | grep httpd #命令行直接运行命令
:09下午 ?? :00.64 /usr/sbin/httpd -D FOREGROUND
:09下午 ?? :00.00 /usr/sbin/httpd -D FOREGROUND
:30下午 ttys002 :00.01 grep httpd
~/tmp/test $ php index.php #使用php查询
Array
(
[] => :09下午 ?? :00.64 /usr/sbin/httpd -D FOREGROUND
[] => :09下午 ?? :00.00 /usr/sbin/httpd -D FOREGROUND
[] => :30下午 ttys002 :00.00 sh -c ps -ef | grep httpd
[] => :30下午 ttys002 :00.00 grep httpd
)
Array
(
[] =>
)
  可以从上面的运行结果中就可以知道为什么要减2

最新文章

  1. showModalDialog打开页面有缓存,不走action
  2. LeetCode 【318. Maximum Product of Word Lengths】
  3. VIM 代码折叠
  4. sql-and、or
  5. 【转】CodeIgniter配置之database
  6. git提交
  7. 238. Product of Array Except Self
  8. 《挑战程序设计竞赛》 4.1.1 矩阵 P286
  9. JS调用PHP 和 PHP调用JS的方法举例
  10. Page Cache buffer Cache
  11. JIRA搭建
  12. SVM入门(一)
  13. [HNOI2013]比赛
  14. MAC下用homebrew安装及配置apache、php和mysql
  15. Linux 下的两个特殊的文件 -- /dev/null 和 /dev/zero 简介及对比
  16. Typora使用说明(记录总结)
  17. 25.75k8s
  18. 动态加载、移除js、css
  19. Linux学习笔记之Linux运行脚本时 $&#39;\r&#39; 错误
  20. Linux renew ip command

热门文章

  1. python_反射——根据字符串获取模块中的属性
  2. uni-app之导航配置pages.json
  3. [Javascript] Construct a Regex to Match Twitter Mentions with Regexr
  4. 最短路--SPFA及其优化
  5. HTML 文字剧中
  6. Oracle 全部后台进程简介
  7. MondoDB介绍 Python与MongoDB用法,安装PyMongo
  8. java上传1t文件
  9. Educational Codeforces Round 70
  10. P3038 [USACO11DEC]牧草种植Grass Planting