bash - pgrep 进程计数显示额外计数
问题描述
我有一个脚本名称 server.sh
#!/bin/bash
process_count=$(ps aux | grep server.sh | grep -v grep | wc -l )
echo "total process running:"
echo $process_count
... other script code
当我运行脚本时,我得到的输出为
./server.sh
total process running:
2
为什么我的进程计数为 2 而不是 1?我只运行了一个脚本,并且还排除了 grep 进程。即使使用 pgrep -f server.sh 并排除 pgrep 也会给出 2 作为进程计数。
解决方案
My approach is to redirect the output of ps
into a temp file, then grep that file. That will eliminate extra processes.
ps aux > /tmp/ps.txt
process_count=$(grep server.sh /tmp/ps.txt | wc -l)
rm /tmp/ps.txt
推荐阅读
- flutter - 用 freeze 定义多态 copyWith 方法
- ios - 如何使用 DateComponentsFormatter 在 HH:MM 中格式化从 00:00 开始的时间间隔偏移量
- date - 显示/打印显示枢轴点日期的标签
- php - PHP 电子邮件:尽管发送了电子邮件,但没有显示任何数据
- javascript - HTML 中的字符串 (es6_string_html) -> 错误:语法错误 jQuery(1.9.1)
- r - 有没有一种快速的方法来比较 R 中的两个向量列表?
- reactjs - Reactjs、Nextjs、Nestjs、GraphQL、PostgreSQL、RabbitMQ 和 Kafka 如何协同工作?
- node.js - 从 api 中提取数据并存储在 mongo
- r - 基于 r 中的两组随机抽样
- findbugs - 如果 MapStruct 中的源值为 null,则抛出 NPE