Results 1 to 2 of 2

Thread: cpu usage in notification

  1. #1
    Join Date
    Dec 2007
    Beans
    7,202
    Distro
    Lubuntu 14.04 Trusty Tahr

    cpu usage in notification

    In the script below, is it possible to have the value of NR==2 as part of the notification in place of "value" so that the notification would look like:
    CPU alert
    CPU 8%
    for example?

    Code:
    #!/usr/bin/env bash
    
    sleep_period=8m 
    
    while true; do
      if ps -eo %C --sort -%cpu | head -2 | awk 'NR==2 { exit !($1>8); }'; then
          notify-send 'CPU alert!' 'CPU usage value%'
      fi
      sleep ${sleep_period}
    done
    de gustibus et coloribus non est disputandum -- Wiktionary

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,493
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: cpu usage in notification

    you should simply store the output of ... '{ print int($1) }' and test it against the threshold in bash and then use the value.

    Code:
    t=8
    cpu=$( ps -eo %C --sort -%cpu | head -2 | awk 'NR==2 { print int($1); }'
    (( cpu>t )) && notify-send 'CPU alert!' 'CPU $cpu%'
    That said, it's possible to hack it out using the exit code
    Code:
    t=8
    if ps -eo %C --sort -%cpu | head -2 | awk -vt="$t" 'NR==2 { exit ($1>t)?int($1):0; }'
    then
        :
    else
        notify-send 'CPU alert!' 'CPU $?%'
    fi
    Last edited by Vaphell; October 16th, 2013 at 10:45 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •