Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: How-to: Screen saver inhibitor for Firefox flash player container-plugin or whatever...

  1. #1

    Default How-to: Screen saver inhibitor for Firefox flash player container-plugin or whatever...

    Hi, I want to share my findings. This is a versatile script to inhibit the screen-saver automatic activation by measuring the CPU consumption of a process.
    Of course I did not wrote the script, all credits goes to the writer Milosz Galazka and a gentleman called Etan Reisner, who help me fix a bug in it.
    Code:
    #!/bin/sh
    # Simple script to demonstrate D-Bus usage
    
    while true
    do
      # read Firefox's container plugin cpu usage
      
      ret=`top -b -n1 -u $(whoami) | awk '$12 ~ /plugin-containe/ {SUM += $9} END {print int(SUM)}'`
    
      if [ -n "$ret" ] && [ "$ret" -gt 5 ]; then
        idle_time=`qdbus org.kde.screensaver /ScreenSaver GetSessionIdleTime`
        if [ "$idle_time" -gt 50 ]; then
          qdbus org.kde.screensaver /ScreenSaver SimulateUserActivity
        fi
      fi
    
      sleep 50
    done
    "awk '$12" is the twelfth column in "top" = process name and "SUM += $9" is the ninth column, thus CPU usage.

    You can edit those four variables:

    plugin-containe : for any process you wish to monitor

    [ "$ret" -gt 5 ] : is the CPU threshold

    [ "$idle_time" -gt 50 ] : is the session idle time

    sleep 50 : is of course, the delay for the script repetition.


    I use OpenSUSE 13.2 with KDE4, for me, I only ad the script to Autostart in System Settings/ Startup and Shutdown.

    Thats all.

  2. #2
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: How-to: Screen saver inhibitor for Firefox flash player container-pluginor whatever...

    On 2015-07-03 05:26, noital wrote:
    >
    > Hi, I want to share my findings. This is a versatile script to inhibit
    > the screen-saver automatic activation by measuring the CPU consumption
    > of a process.


    Interesting idea!


    > Code:
    > --------------------


    > qdbus org.kde.screensaver /ScreenSaver SimulateUserActivity


    > --------------------


    Thus it will only work for kde.

    --
    Cheers / Saludos,

    Carlos E. R.

    (from 13.1 x86_64 "Bottle" (Minas Tirith))

  3. #3

    Default Re: How-to: Screen saver inhibitor for Firefox flash player container-pluginor whatever...

    Quote Originally Posted by robin_listas View Post
    On 2015-07-03 05:26, noital wrote:
    >
    > Hi, I want to share my findings. This is a versatile script to inhibit
    > the screen-saver automatic activation by measuring the CPU consumption
    > of a process.


    Interesting idea!


    > Code:
    > --------------------


    > qdbus org.kde.screensaver /ScreenSaver SimulateUserActivity


    > --------------------



    Thus it will only work for kde.

    --
    Cheers / Saludos,

    Carlos E. R.

    (from 13.1 x86_64 "Bottle" (Minas Tirith))
    You are right, it's unfortunately too late to change the title.

    But I guess, if you edit the dbus command for some gnome equivalent, it will work. I never used gnome, I could not tell.

    Regard

  4. #4

    Default Re: How-to: Screen saver inhibitor for Firefox flash player container-pluginor whatever...

    Quote Originally Posted by noital View Post
    But I guess, if you edit the dbus command for some gnome equivalent, it will work. I never used gnome, I could not tell.
    Use "org.freedesktop.ScreenSaver", that should work for both KDE and GNOME, and probably other DEs as well.
    Code:
    qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity
    Or see here for how to temporary disable/enable it:
    http://people.freedesktop.org/~hades...spec/re01.html

    I.e. something like
    Code:
    cookie=`qdbus org.freedesktop.ScreenSaver /ScreenSaver Inhibit someId reason`
    (where someId should uniquely identify your application, and reason is a string that describes the reason)

    Afterwards you can enable it again with:
    Code:
    qdbus org.freedesktop.ScreenSaver /ScreenSaver UnInhibit $cookie
    Last edited by wolfi323; 03-Jul-2015 at 09:45.

  5. #5
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: How-to: Screen saver inhibitor for Firefox flash player container-pluginor whatever...

    On 2015-07-03 18:46, wolfi323 wrote:

    > Use "org.freedesktop.ScreenSaver", that should work for both KDE and
    > GNOME, and probably other DEs as well.


    But you need have qdbus installed, which is part of qt. I have it, so it
    is no problem, but... :-?

    I have to experiment with this :-)

    --
    Cheers / Saludos,

    Carlos E. R.

    (from 13.1 x86_64 "Bottle" (Minas Tirith))

  6. #6

    Default Re: How-to: Screen saver inhibitor for Firefox flash player container-pluginor whatever...

    Quote Originally Posted by


    But you need have qdbus installed, which is part of qt. I have it, so it
    is no problem, but... :-?

    I have to experiment with this :-)

    --
    Cheers / Saludos,

    Carlos E. R.[/QUOTE

    (from 13.1 x86_64 "Bottle" (Minas Tirith))
    I have found this command:

    Code:
    dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.SimulateUserActivity
    I didn’t found the "GetSessionIdleTime" command. But if the above command works in a terminal without error. My understanding is that getting session idle time is optional and modifying the script without it is possible and would still do the job:

    Code:
    #!/bin/sh
    # Simple script to demonstrate D-Bus usage
    
    while true
    do
      # read google chrome cpu usage
      ret=`top -b -n1 -u $(whoami) | awk '$12 ~ /plugin-containe/ {SUM += $9} END {print int(SUM)}'`
    
      if [ -n "$ret" ] && [ "$ret" -gt 5 ]; then
    
          dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.SimulateUserActivity
        
      fi
    
      sleep 50
    done
    Because the script run every 50 secs as it is and can be edit to more or less.

    Regards

  7. #7

    Default Re: How-to: Screen saver inhibitor for Firefox flash player container-pluginor whatever...

    Quote Originally Posted by robin_listas View Post
    On 2015-07-03 18:46, wolfi323 wrote:

    > Use "org.freedesktop.ScreenSaver", that should work for both KDE and
    > GNOME, and probably other DEs as well.


    But you need have qdbus installed, which is part of qt. I have it, so it
    is no problem, but... :-?

    I have to experiment with this :-)
    As mentioned already by noital, you can use dbus-send as well, which is part of dbus itself, so is available everywhere.
    But the syntax is a bit different...

    E.g. this should work on any Linux DE (or many at least):
    Code:
    dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver /org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.SimulateUserActivity

  8. #8
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: How-to: Screen saver inhibitor for Firefox flash player container-pluginor whatever...

    On 2015-07-03 21:46, wolfi323 wrote:


    Ok, I'm trying this. I wrote the script this way:


    Code:
    #!/bin/bash
    
    while true
    do
    # read Firefox's container plugin cpu usage
    
    DATE=`date --rfc-3339=seconds`
    DATE=`date  --iso=ns`
    
    ret=`top -b -n1 -u $(whoami) | awk '$12 ~ /plugin-containe/ {SUM += $9} END {print int(SUM)}'`
    
    if [ -n "$ret" ] && [ "$ret" -gt 5 ]; then
    echo "$DATE Simulando actividad ($ret)"
    dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver \
    /org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.SimulateUserActivity
    else
    echo "$DATE No hago nada ($ret)"
    fi
    sleep 50
    done

    Then I started plying something, but it detects no activity. Thus I tried separately:


    Code:
    cer@minas-tirith:~> top -b -n1 -u $(whoami) | grep plugin-con
    30058 cer       20   0  930416 132748  35216      0 132748 R 69.68 3.354   2:08.77 plugin-containe
    cer@minas-tirith:~> top -b -n1 -u $(whoami) | awk '$12 ~ /plugin-containe/ {SUM += $9} END {print int(SUM)}'
    0
    cer@minas-tirith:~>

    You see, CPU is about 70%, yet the code says it is zero.
    I don't know awk, so I can't trace the issue :-?

    I think I know. My "top" is configured to print other fields:


    Code:
    top - 22:55:17 up 11 days, 8 min, 30 users,  load average: 0.49, 1.09, 1.02
    Tasks: 288 total,   1 running, 287 sleeping,   0 stopped,   0 zombie
    %Cpu(s): 13.2 us,  3.2 sy,  0.0 ni, 80.5 id,  3.2 wa,  0.0 hi,  0.0 si,  0.0 st
    KiB Mem:   3957996 total,  2170292 used,  1787704 free,    64372 buffers
    KiB Swap:  6289412 total,  1270268 used,  5019144 free,   876920 cached
    
    PID USER      PR  NI    VIRT    RES    SHR   SWAP   USED S  %CPU  %MEM     TIME+ COMMAND
    8256 cer       20   0 2289440 532404  28488 295976 828380 S 12.88 13.45 185:56.74 firefox

    So, alternatives:
    - customizing the awk line so that it reads the proper field for "me".
    - same, but first it has to read columns headers to locate the right one.
    - use something else altogether. Perhaps "ps --format".

    This one looks promising:


    Code:
    ps -xo pcpu,user,fname,comm

    Further tuning is possible, but I can't quite get it.


    Code:
    cer@minas-tirith:~> ps -xo pcpu,user,fname,comm | grep plugin
    51.3 cer      plugin-c plugin-containe
    cer@minas-tirith:~>
    
    cer@minas-tirith:~> ps -C plugin-containe -xo pcpu,user,fname,comm
    error: must set personality to get -x option
    
    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).
    cer@minas-tirith:~>
    
    cer@minas-tirith:~> ps -C plugin-containe
    PID TTY          TIME CMD
    30785 ?        00:02:16 plugin-containe
    cer@minas-tirith:~>

    You see, it is possible to tell ps to print data for a single process by name;
    and it is also possible to print the fields we want and in the order we want;
    But I can't do both. :-?


    --
    Cheers / Saludos,

    Carlos E. R.

    (from 13.1 x86_64 "Bottle" (Minas Tirith))

  9. #9

    Default Re: How-to: Screen saver inhibitor for Firefox flash player container-plugin or whatever...

    Maybe I’m missing something, but as I mentioned in the original post:

    "awk '$12" is the twelfth column in "top" = process name and "SUM += $9" is the ninth column, thus CPU usage.

    I see 14 in your "top" so "awk '$14" would be the process "SUM += $11" would be the CPU usage.

  10. #10
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: How-to: Screen saver inhibitor for Firefox flash player container-pluginor whatever...

    On 2015-07-04 00:46, noital wrote:
    >
    > Maybe I’m missing something, but as I mentioned in the original post:
    >
    > "awk '$12" is the twelfth column in "top" = process name and "SUM +=
    > $9" is the ninth column, thus CPU usage.
    >
    > I see 14 in your "top" so "awk '$14" would be the process "awk '$11"
    > would be the CPU usage.


    That's the point, that the code is not generic, it has to be modified
    for each user. I'm trying to find some other method to function for any
    user.

    --
    Cheers / Saludos,

    Carlos E. R.

    (from 13.1 x86_64 "Bottle" (Minas Tirith))

Page 1 of 3 123 LastLast

Posting Permissions

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