You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash脚本中获取sol3-script-ui进程数与命令行结果不符的原因及解决方法

Why does the process count differ between terminal and bash script?

Let's break down what's happening here, and how to fix it to get the correct 8 count in your script.

The Problem

When you run the ps aux | grep ... pipeline directly in your terminal, you get the correct 8 processes matching sol3-script-ui. But when you wrap this exact command in a bash script, the output jumps to 10. What's causing this discrepancy?

Root Cause

The extra 2 counts come from processes spawned by your script itself that get caught by your grep filter:

  1. The bash process running your script: Its command line is /bin/bash ./get-sol3-script-ui-nr.sh, which includes the string "sol3-script-ui" — so your grep picks it up as a match.
  2. The grep "sol3-script-ui" process from your pipeline: Even though you added grep -v grep to exclude grep processes, this only filters out lines containing the word "grep". The grep process's command line still has "sol3-script-ui", so it gets matched before the exclusion step.

Together, these two unintended matches add 2 to your original 8, resulting in the incorrect 10 count.

Fixes to Get the Correct Count

1. Use pgrep (The Easiest, Most Reliable Way)

pgrep is purpose-built for counting processes by their actual executable name, and it automatically ignores the script and grep processes (since it doesn't match command line arguments, just the process name).

Update your script to this:

#!/bin/bash
sol3Nr=$(pgrep -c sol3-script-ui)
echo $sol3Nr

This will consistently return the correct 8 count without any extra noise.

2. Tweak Your Grep Pattern

If you want to stick with your original pipeline, use a regex trick to avoid matching the grep process itself:

#!/bin/bash
sol3Nr=$(ps aux | grep "[s]ol3-script-ui" | grep -v grep | awk '{print $2}' | wc -l)
echo $sol3Nr

The [s] tells grep to match the letter s, so it still finds your target sol3-script-ui processes. But the grep process's command line will be grep "[s]ol3-script-ui" — which doesn't contain the exact string "sol3-script-ui", so it won't be matched. For extra safety, you can add a filter to exclude the script itself:

#!/bin/bash
sol3Nr=$(ps aux | grep "[s]ol3-script-ui" | grep -v grep | grep -v "get-sol3-script-ui-nr.sh" | awk '{print $2}' | wc -l)
echo $sol3Nr

This works, but it's more fragile than using pgrep.

3. Use ps Built-In Filtering

You can cut out grep entirely by using ps's native process filtering:

#!/bin/bash
sol3Nr=$(ps -C sol3-script-ui --no-headers | wc -l)
echo $sol3Nr

The -C sol3-script-ui flag tells ps to only show processes named sol3-script-ui, and --no-headers removes the top header line so wc -l counts only the actual process lines.

内容的提问来源于stack exchange,提问作者XYZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 20:19:08