skip to Main Content

I’m trying to build a script in Linux (Debian 10) that shows the net usage (%) of a process passed as an argument.
This is the code, but there isn’t any output:

ProcessName=$1
(nethogs -t ens33 | awk '/$ProcessName/{print $3}') &> output.txt

2

Answers


  1. What I’m doing wrong?

    What you’re doing wrong is single-quoting $ProcessName while wanting this to be parameter-expanded. To get the expansion, just don’t quote there, e. g.:

    … awk /$ProcessName/'{print $3}' …
    
    Login or Signup to reply.
  2. While using tracemode nethogs -t, first field of output is program and it can consists of irregular number of arguments.
    In case of brave:

    /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
    

    so $3 will no longer be as expected, you need to get last column of output using $(NF) as follow:

    ... | awk /$ProcessName/'{print $(NF)}'
    

    for second last column:

    ... | awk /$ProcessName/'{print $(NF - 1)}'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search