[英]Running a shell script to get correct process count
I am trying to run the below shell script test.sh
我正在尝试运行以下shell脚本
test.sh
$service=$1
$count=`ps -ef |grep -i "$service" |grep -v grep | wc -l`
echo "$count"
Command: sh test.sh abcde
命令:
sh test.sh abcde
I am expecting the script to output 0 but it gives me 1. 我期望脚本输出0,但它给我1。
PS: I will be running this script using shell_exec from a php file and input to script will be array elements from php file PS:我将使用PHP文件中的shell_exec运行此脚本,脚本的输入将是PHP文件中的数组元素
You get 1
because the output of ps -ef
include the command 您得到
1
是因为ps -ef
的输出包括以下命令
sh test.sh abcde
and this is matched when you do grep -i "abcde"
. 这与您执行
grep -i "abcde"
时匹配。 You need to filter this out for the same reason you filter out grep
, so change 出于过滤
grep
的相同原因,您需要对此进行过滤,因此请更改
grep -v grep
to 至
grep -E -v 'grep|test\.sh'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.