Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Script to disable screensaver when CPU usage exceeds a certain percentage ?

  1. #1
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,685
    Distro
    Lubuntu 14.04 Trusty Tahr

    Script to disable screensaver when CPU usage exceeds a certain percentage ?

    I have this:
    Code:
    #!/usr/bin/env bash
    
    sleep_period=7m #minutes
    
    while true; do
      if [[ $(pidof mplayer | wc -w) -gt 0 ]]; then
        while [[ $(pidof mplayer | wc -w) -gt 0 ]]; do
          xdotool mousemove 0 100
          sleep ${sleep_period}
        done
      else
        sleep ${sleep_period}
      fi
    done
    It works for GNOME Mplayer but I'd like a more general solution.
    de gustibus et coloribus non est disputandum -- Wiktionary

  2. #2
    Join Date
    Oct 2010
    Location
    Above ground...(for now)
    Beans
    761

    Re: Script to disable screensaver when CPU usage exceeds a certain percentage ?

    Quote Originally Posted by vasa1 View Post
    ...It works for GNOME Mplayer but I'd like a more general solution.
    There are several different ways you can capture your CPU usage.
    This might help: http://www.cyberciti.biz/tips/how-do...ilization.html
    Suffering from severe chronic female supervision deficiency syndrome resulting in
    an increasing intolerance of adolescent scatological effluence and PMS induced nefarious diatribe.
    How to: Mark your thread as: [SOLVED]

  3. #3
    Join Date
    Jun 2009
    Location
    0:0:0:0:0:0:0:1
    Beans
    4,110
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: Script to disable screensaver when CPU usage exceeds a certain percentage ?

    mplayer has this feature built in
    to enable it run this:
    Code:
    echo 'heartbeat-cmd="xscreensaver-command -deactivate"' >> ~/.mplayer/config
    i use this for other players like adobe flash
    Code:
    #!/bin/bash
    me="`dirname $0`/`basename $0`"
    #echo $me
    if [ "$1" == "--help" ];then
        echo -e "Usage:\n\t`basename $0` [CPU TIMER] | --debug | --help\n\tCPU is the cpu usage usage for flash times 10 to assume video is running\n\tTimer is the amount of time to wait before checking if a video player is running"
        exit
    elif [ "$1" == "--debug" ];then
        cpu="20"
        delay="5"
    else
        if [ -z "$1" ];then
            cpu="20"
        else
            cpu="$1"
        fi
        if [ -z "$2" ];then
            delay="55"
        else
            delay="$2"
        fi
        if [ "`pgrep -f $me | wc -l`" -gt "2" ]; then
            echo "Already running..."
            exit
        fi
    fi
    while [ 1 ]; do
    #    clear
        chrome="`pgrep -lf chrome | grep ppapi | awk '{print $1}' | paste -sd ','`"
        flash="`pgrep -f flashplayer | paste -sd ','`"
    #    echo "chrome=$chrome"
    #    echo "flash=$flash"
        if [[ -n "$chrome" || -n "$flash" ]];then
            if [ -n "$chrome" ] && [ -n "$flash" ];then
                flash="$flash,$chrome"
            elif [ -n "$chrome" ];then
                flash="$chrome"
            fi
    #        echo "Flash processes: $flash"
            # multiply by 10 to remove decimal in newer versions of top
            flash=`top -bn 1 -p "$flash" | grep $USER | head -1 | awk '{print $9*10}'`
            if [ "$1" == "--debug" ];then
                echo "Flash is using `calc $flash/10`% CPU. Threshold is `calc $cpu/10`%"
            fi
        else
    #        echo "No flash running"
            flash="0"
        fi
        if [[ $flash -gt $cpu || -n "`pgrep -f parole`" ]]; then
            xscreensaver-command -deactivate > /dev/null
            if [ `xset -q | grep -ce 'DPMS is Enabled'` -eq 1 ];then
                xset -dpms
                xset dpms
            fi
            echo "Video or flash is running, no screensaver for you"
        fi
        sleep $delay
    done
    Laptop: ASUS A54C-NB91 (Storage: WD3200BEKT + MKNSSDCR60GB-DX); Desktop: Custom Build - Images included; rPi Server
    Putting your Networked Printer's scanner software to shame PHP Scanner Server
    I frequently edit my post when I have the last post

  4. #4
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,685
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: Script to disable screensaver when CPU usage exceeds a certain percentage ?

    Thanks for the answers. After a while of no responses here, I cooked up something else, ran into trouble, and posted at unix.stackexchange.com: Script to prevent screen blanking using “mouse move” doesn't work. Based on answers there, what I'm using now looks like this:

    Code:
    #!/usr/bin/env bash
    
    sleep_period=60s 
    
    while true; do
      if [[ $(top -bn 1 | sed -nrs '8p' | awk '{ print int($9) }') -gt 5 ]]; then 
        while [[ $(top -bn 1 | sed -nrs '8p' | awk '{ print int($9) }') -gt 5 ]]; do
          xset -dpms; xset s off
          xset +dpms; xset s on
          sleep ${sleep_period}
        done
      else
        sleep ${sleep_period}
      fi
    done
    I know the sleep_period is short and the CPU% is low but I'll change those later. And no "mouse move".
    de gustibus et coloribus non est disputandum -- Wiktionary

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,278
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Script to disable screensaver when CPU usage exceeds a certain percentage ?

    isn't that if superficial? It's function is performed by the inner while either way
    Code:
    while true; do
      while top -bn 1 | awk 'NR==8 { exit !($9>5); }'; do
        xset -dpms; xset s off
        xset +dpms; xset s on
        sleep ${sleep_period}
      done
      sleep ${sleep_period}
    done
    should do roughly the same thing
    Last edited by Vaphell; September 19th, 2013 at 06:37 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

  6. #6
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,685
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: Script to disable screensaver when CPU usage exceeds a certain percentage ?

    Quote Originally Posted by Vaphell View Post
    isn't that if superficial? It's function is performed by the inner while either way
    ...
    should do roughly the same thing
    I'm just a humble newbie in all this

    I'll give your script a try. Your previous advice has been very helpful!
    de gustibus et coloribus non est disputandum -- Wiktionary

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,278
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Script to disable screensaver when CPU usage exceeds a certain percentage ?

    another attempt, while loop inside while loop still stank
    Code:
    while true; do
      if top -bn 1 | awk 'NR==8 { exit !($9>5); }'; then
        xset -dpms; xset s off
        xset +dpms; xset s on
      fi
      sleep ${sleep_period}
    done
    Last edited by Vaphell; September 19th, 2013 at 06:34 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

  8. #8
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,685
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: Script to disable screensaver when CPU usage exceeds a certain percentage ?

    Quote Originally Posted by vasa1 View Post
    I'll give your script a try. ...
    I tried:
    Code:
    #!/usr/bin/env bash
    
    sleep_period=5m 
    
    while true; do
      while top -bn 1 | awk 'NR==8 { exit !($9>8); }'; do
        xset -dpms; xset s off
        xset +dpms; xset s on
        sleep ${sleep_period}
      done
      sleep $
    done
    but I get
    Code:
    [11:02 AM] ~/bin $ vh.sh
    sleep: invalid time interval ‘$’
    Try 'sleep --help' for more information.
    sleep: invalid time interval ‘$’
    Try 'sleep --help' for more information.
    sleep: invalid time interval ‘$’
    Try 'sleep --help' for more information.
    ...
    de gustibus et coloribus non est disputandum -- Wiktionary

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,278
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Script to disable screensaver when CPU usage exceeds a certain percentage ?

    obviously i ate {sleep_period}
    either way, try newer version without inner loop
    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

  10. #10
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,685
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: Script to disable screensaver when CPU usage exceeds a certain percentage ?

    Quote Originally Posted by Vaphell View Post
    another attempt, while loop inside while loop still stank
    Code:
    while true; do
      if top -bn 1 | awk 'NR==8 { exit !($9>5); }'; then
        xset -dpms; xset s off
        xset +dpms; xset s on
      fi
      sleep ${sleep_period}
    done
    No error in terminal so far
    de gustibus et coloribus non est disputandum -- Wiktionary

Page 1 of 2 12 LastLast

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
  •