Linux 中进程的净使用率 (%)
Net Usage (%) of a Process in Linux
我正在尝试在 Linux (Debian 10) 中构建一个脚本,该脚本显示作为参数传递的进程的净使用率 (%)。
这是代码,但没有任何输出:
ProcessName=
(nethogs -t ens33 | awk '/$ProcessName/{print }') &> output.txt
What I'm doing wrong?
你做错的是单引号 $ProcessName
同时希望它被参数扩展。要获得扩展,只是不要在那里引用,e。 g.:
… awk /$ProcessName/'{print }' …
使用跟踪模式nethogs -t
时,输出的第一个字段是程序,它可以包含不规则数量的参数。
如遇勇者:
/usr/lib/brave-bin/brave --type=utility --utility-sub-type=network.mojom.NetworkService --field-trial-handle=18208005703828410459,4915436466583499460,131072 --enable-features=AutoupgradeMixedContent,DnsOverHttps,LegacyTLSEnforced,PasswordImport,PrefetchPrivacyChanges,ReducedReferrerGranularity,SafetyTip,WebUIDarkMode --disable-features=AutofillEnableAccountWalletStorage,AutofillServerCommunication,DirectSockets,EnableProfilePickerOnStartup,IdleDetection,LangClientHintHeader,NetworkTimeServiceQuerying,NotificationTriggers,SafeBrowsingEnhancedProtection,SafeBrowsingEnhancedProtectionMessageInInterstitials,SharingQRCodeGenerator,SignedExchangePrefetchCacheForNavigations,SignedExchangeSubresourcePrefetch,SubresourceWebBundles,TabHoverCards,TextFragmentAnchor,WebOTP --lang=en-US --service-sandbox-type=none --shared-files=v8_context_snapshot_data:100/930/1000 0.0554687 0.0554687
所以 </code> 将不再像预期的那样,您需要使用 <code>$(NF)
获取最后一列输出,如下所示:
... | awk /$ProcessName/'{print $(NF)}'
倒数第二列:
... | awk /$ProcessName/'{print $(NF - 1)}'
我正在尝试在 Linux (Debian 10) 中构建一个脚本,该脚本显示作为参数传递的进程的净使用率 (%)。 这是代码,但没有任何输出:
ProcessName=
(nethogs -t ens33 | awk '/$ProcessName/{print }') &> output.txt
What I'm doing wrong?
你做错的是单引号 $ProcessName
同时希望它被参数扩展。要获得扩展,只是不要在那里引用,e。 g.:
… awk /$ProcessName/'{print }' …
使用跟踪模式nethogs -t
时,输出的第一个字段是程序,它可以包含不规则数量的参数。
如遇勇者:
/usr/lib/brave-bin/brave --type=utility --utility-sub-type=network.mojom.NetworkService --field-trial-handle=18208005703828410459,4915436466583499460,131072 --enable-features=AutoupgradeMixedContent,DnsOverHttps,LegacyTLSEnforced,PasswordImport,PrefetchPrivacyChanges,ReducedReferrerGranularity,SafetyTip,WebUIDarkMode --disable-features=AutofillEnableAccountWalletStorage,AutofillServerCommunication,DirectSockets,EnableProfilePickerOnStartup,IdleDetection,LangClientHintHeader,NetworkTimeServiceQuerying,NotificationTriggers,SafeBrowsingEnhancedProtection,SafeBrowsingEnhancedProtectionMessageInInterstitials,SharingQRCodeGenerator,SignedExchangePrefetchCacheForNavigations,SignedExchangeSubresourcePrefetch,SubresourceWebBundles,TabHoverCards,TextFragmentAnchor,WebOTP --lang=en-US --service-sandbox-type=none --shared-files=v8_context_snapshot_data:100/930/1000 0.0554687 0.0554687
所以 </code> 将不再像预期的那样,您需要使用 <code>$(NF)
获取最后一列输出,如下所示:
... | awk /$ProcessName/'{print $(NF)}'
倒数第二列:
... | awk /$ProcessName/'{print $(NF - 1)}'