如果你有pgrep
,使用pgrep -x mysqld
將是更好的選擇比ps + grep
從man pgrep
pgrep, pkill - look up or signal processes based on name and other attributes
-x, --exact
Only match processes whose names (or command line if -f is specified) exactly match the pattern.
-l, --list-name
List the process name as well as the process ID. (pgrep only.)
-c, --count
Suppress normal output; instead print a count of matching processes. When count does not match any‐
thing, e.g. returns zero, the command will return non-zero value.
-n, --newest
Select only the newest (most recently started) of the matching processes.
嘛,'grep的「\ bmysqld \ b''不能在'mysqld_safe'中找到'mysqld'。僅僅因爲'_'是一個單詞char,'d'和'_'之間沒有單詞邊界。 –
假設bash,你必須單引號的正則表達式。就目前而言,grep看到的正則表達式2是'bmysqlb',不應該匹配任何這些行。 [「不加引號的反斜槓(\\)是轉義字符,它保留了下一個字符的文字值](https://linux.die.net/man/1/bash) –
這不是作業,我試圖解決監控問題。我想先過濾這個,然後再用json輸出一個軟件。另外\ b是我在幾分鐘前學過的東西,然後在這裏發佈問題。有很多類似的問題,我經歷了其中的大部分,這就是我遇到grep邊界的原因。 – anon