LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I run a scripts when screen saver activates? (https://www.linuxquestions.org/questions/linux-software-2/how-do-i-run-a-scripts-when-screen-saver-activates-623297/)

MikeyCarter 02-23-2008 10:29 AM

How do I run a scripts when screen saver activates?
 
I have a scripts, say foo.sh, I want to activate when the screen saver is active and deactivate when the user comes back on line.

Basically foo.sh is some custom processing I want to do in the computer idle times. I figure hooking into the screen saver or monitor power saver mode some how would be the ideal way to detect this.

Does anyone have any suggestions.

win32sux 02-24-2008 04:56 AM

I don't know which script would need to be edited in order to stick your command in it (I too would like to find out), but I just thought I would mention that there's also another way you could approach this: Have a cron job run every minute checking to see whether a screensaver is running. If there is, your program gets started; if there's not (and your program is running), your program is stopped; if there's not (and your program isn't running), nothing is done.

Yes, this method sucks compared to what you currently have in mind, but it's an alternative nevertheless. Like I said, I too look forward to learning which script to edit in order to do this in a more kosher way. I don't have anything serious which I'd want to execute when the screensaver comes on but making an ogg/mp3 play would be cool. :)

MikeyCarter 02-24-2008 10:59 AM

Good I idea... but if I was going to create a script like that I'd want to base it on more than the screen saver.

ie: what if the computer is on but no users are logged in. Also account for system load.


I have discovered something about gnome screen savers. Creating them is very easy. I could design a new screen saver which calls say slideshow and my program. So far I've just found this... I haven't actually played with it yet.

Code:

[mcarter@snow-faerie screensavers]$ cat /usr/share/applications/screensavers/frogs-slideshow.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Frogs
Comment=Display a slideshow of pictures of frogs
Exec=slideshow --location=/usr/share/pixmaps/backgrounds/frogs/
TryExec=slideshow
StartupNotify=false
Terminal=false
Type=Application
Categories=GNOME;Screensaver;

Programs run by the screen saver are in /usr/libexec
[mcarter@snow-faerie screensavers]$ ls -l /usr/libexec/gnome-screensaver/       
total 96
-rwxr-xr-x 1 root root 21748 2007-11-12 16:34 floaters
-rwxr-xr-x 1 root root    34 2007-10-02 10:23 f-spot-screensaver
-rwxr-xr-x 1 root root 17688 2007-11-12 16:34 popsquares
-rwxr-xr-x 1 root root 29744 2007-11-12 16:34 slideshow


MikeyCarter 02-25-2008 10:57 AM

Well no luck on the screen saver front. However I did find something to work. My script runs through a loop. The processing maxes the CPU for about 2 seconds. Then moves to the next.

So I put in a check after each processing to see if the tty has been idle for more than 15 minutes. (main command being "w") Does the trick. I just wish there was a better way to handle time.

Code:

      echo "Pausing for Computer to be idle"
      ########################################################
      # Check if the PC (tty console) is idle.  If idle for
      # more than 15 minutes.  If so do the run.  For this to
      # work script needs to be started with the pc on reboot
      ########################################################
      let IdleYN=0
      while [ $IdleYN -eq 0 ] ; do
        IdleString=`w | grep tty | awk {'print $5'}`
        let IdleYN=1

        ################################################################
        # Loop through all users who are currently idle
        ################################################################
        for IdleTime in $IdleString ; do
          let Idle=0
          let Minutes=0

          if [ "`echo $IdleTime | grep "s"`" != "" ] ; then
                  let Idle=0
          else
            if [ "`echo $IdleTime | grep -e "m" -e "day"`" == "" ] ; then
              let Minutes=`echo $IdleTime | awk -F: {'print $1'}`
              if [ $Minutes -gt 15 ] ; then
                  let Idle=1
              else
                  let Idle=0
              fi
            else
              let Idle=1
            fi
          fi


          ################################################################
          # If the Idle time is 0 for any tty console set the master to 0
          # That way if two users are logged in it will only run if all
          # users are idle
          ################################################################
          if [ $Idle -eq 0 ] ; then
                let IdleYN=0
          fi

        done

        if [ $IdleYN -eq 0 ] ; then
          sleep 15
        fi
      done


Slick666 07-01-2008 10:31 AM

I'm looking to do the same. I want the machine to do some work when the screensaver is on and stop when the screen saver is off. One idea I had that might work is to base to activation and deactivation on whether the screen is locked or not. I almost ALWAYS lock my screen so I thought I could use that as a trigger but I really don't know how or where to get started looking.

Any thoughts?

MikeyCarter 07-01-2008 11:56 AM

Quote:

Originally Posted by Slick666 (Post 3200375)
I'm looking to do the same. I want the machine to do some work when the screensaver is on and stop when the screen saver is off. One idea I had that might work is to base to activation and deactivation on whether the screen is locked or not. I almost ALWAYS lock my screen so I thought I could use that as a trigger but I really don't know how or where to get started looking.

Any thoughts?


I went a totally different route and looked for when the computer tty sessions were idle for 15 minutes.

Code:

      echo "Pausing for Computer to be idle"
      ########################################################
      # Check if the PC (tty console) is idle.  If idle for
      # more than 15 minutes.  If so do the run.  For this to
      # work script needs to be started with the pc on reboot
      ########################################################
      let IdleYN=0
      while [ $IdleYN -eq 0 ] ; do
        IdleString=`w | grep tty | awk {'print $5'}`
        let IdleYN=1

        ################################################################
        # Loop through all users who are currently idle
        ################################################################
        for IdleTime in $IdleString ; do
          let Idle=0
          let Minutes=0

          if [ "`echo $IdleTime | grep "s"`" != "" ] ; then
                  let Idle=0
          else
            if [ "`echo $IdleTime | grep -e "m" -e "day"`" == "" ] ; then
              let Minutes=`echo $IdleTime | awk -F: {'print $1'}`
              if [ $Minutes -gt 15 ] ; then
                  let Idle=1
              else
                  let Idle=0
              fi
            else
              let Idle=1
            fi
          fi


          ################################################################
          # If the Idle time is 0 for any tty console set the master to 0
          # That way if two users are logged in it will only run if all
          # users are idle
          ################################################################
          if [ $Idle -eq 0 ] ; then
                let IdleYN=0
          fi

        done

        ################################################################
        # Override idle check for testing
        ################################################################
        if [ -s /var/run/cf_geopack_remote.pid ] ; then
                echo "Exists: /var/run/cf_geopack_remote.pid skipping idle check"
                let IdleYN=1
        fi

        if [ $IdleYN -eq 0 ] ; then
          sleep 15
        fi

      done



All times are GMT -5. The time now is 09:29 AM.