查看php 某个服务的进程数
2024-09-01 09:37:15
查看进程就是使用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
最新文章
- showModalDialog打开页面有缓存,不走action
- LeetCode 【318. Maximum Product of Word Lengths】
- VIM 代码折叠
- sql-and、or
- 【转】CodeIgniter配置之database
- git提交
- 238. Product of Array Except Self
- 《挑战程序设计竞赛》 4.1.1 矩阵 P286
- JS调用PHP 和 PHP调用JS的方法举例
- Page Cache buffer Cache
- JIRA搭建
- SVM入门(一)
- [HNOI2013]比赛
- MAC下用homebrew安装及配置apache、php和mysql
- Linux 下的两个特殊的文件 -- /dev/null 和 /dev/zero 简介及对比
- Typora使用说明(记录总结)
- 25.75k8s
- 动态加载、移除js、css
- Linux学习笔记之Linux运行脚本时 $&#39;\r&#39; 错误
- Linux renew ip command
热门文章
- python_反射——根据字符串获取模块中的属性
- uni-app之导航配置pages.json
- [Javascript] Construct a Regex to Match Twitter Mentions with Regexr
- 最短路--SPFA及其优化
- HTML 文字剧中
- Oracle 全部后台进程简介
- MondoDB介绍 Python与MongoDB用法,安装PyMongo
- java上传1t文件
- Educational Codeforces Round 70
- P3038 [USACO11DEC]牧草种植Grass Planting