我正在嘗試獲取 PID 中的行程 ID,然后使用 grep 命令列出的所有行程 ID 獲取 CPU 和記憶體使用情況,但我遇到了錯誤。任何幫助,將不勝感激
#!/bin/bash
PID=`ps -eaf | grep firefox | grep -v grep | awk '{print $2}'`
usage= `ps -p $PID -o %cpu,%mem`
錯誤:
error: process ID list syntax error
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).
uj5u.com熱心網友回復:
這通常在 firefox 未運行時發生:PID 不存在并且您會得到以下行為:
Prompt>ps -p -o %cpu,%mem // as firefox does not run, $PID is empty
error: process ID list syntax error
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).
uj5u.com熱心網友回復:
一次做一個
#!/bin/bash
for PID in `ps -eaf | grep firefox | grep -v grep | awk '{print $2}'` ; do
usage=`ps -p $PID -o %cpu,%mem`
done
uj5u.com熱心網友回復:
獲取csvpid 串列的建議命令pgrep -d, -f firefox。
或者將 pids_list 收集到變數中:
pid_list=$(pgrep -d, -f firefox)
現在使用ps命令提取有關資訊$pid_list
ps -p $pid_list -o %cpu,%mem
或者在一行中:
ps -p $(pgrep -d, -f firefox) %cpu,%mem
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/422209.html
標籤:
上一篇:Linuxbash僅解壓縮一個帶有存檔檔案zip的檔案
下一篇:按日期查找和復制特定檔案
