Results 1 to 8 of 8

Thread: KDE5 switch hibernate / suspend in night / day

  1. #1
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    168

    Question KDE5 switch hibernate / suspend in night / day

    Hi

    I've been looking for a command line way to switch KDE5 power management to suspend or to hibernate, so I can have a script which will, when I wake the computer, set the next sleep according to whether it's day or night, i.e. after 11pm, hibernate at next sleep, after 8am suspend at next sleep.

    Can anyone point me to the right bit of documentation / dbus commands / whatever?

    Thanks
    David

    p.s. I've searched, and there's lots of old stuff that no longer works, but nothing recent I can see ...

  2. #2
    Join Date
    Jun 2008
    Location
    Auckland, NZ
    Posts
    20,286
    Blog Entries
    1

    Default Re: KDE5 switch hibernate / suspend in night / day

    KDE's power management utility can be configured to run a script after a certain period of inactivity. Such a script could check the current time, then suspend ('systemctl suspend' command) or hibernate ('systemctl hibernate' command) as appropriate

    System Settings > Power Management > Energy Saving > Run Script....
    Last edited by deano_ferrari; 17-Oct-2017 at 18:06.

  3. #3
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    168

    Thumbs up Re: KDE5 switch hibernate / suspend in night / day

    Many thanks. I'll do that this weekend.

  4. #4
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    168

    Question Re: KDE5 switch hibernate / suspend in night / day

    OK. Thanks. That works. :-)

    Another related question: I was trying to be clever and not sleep if certain jobs are running (ffmpeg, transcode for media conversion unison, rsync for backups etc.). I have two options for this: return a non-zero return code; or wait until the job has finished and do the sleep then. The former is dependent on whether (and when) the power management software retries later if the sleep fails, and the later would benefit if the script could recognise if someone had removed / closed the screen locker.

    Is there a "universal" (within X11) way to tell if a screen locker has locked the screen?

    Thanks
    David

  5. #5
    Join Date
    Jun 2008
    Location
    Auckland, NZ
    Posts
    20,286
    Blog Entries
    1

    Default Re: KDE5 switch hibernate / suspend in night / day

    Quote Originally Posted by jetojedno View Post
    OK. Thanks. That works. :-)

    Another related question: I was trying to be clever and not sleep if certain jobs are running (ffmpeg, transcode for media conversion unison, rsync for backups etc.). I have two options for this: return a non-zero return code; or wait until the job has finished and do the sleep then. The former is dependent on whether (and when) the power management software retries later if the sleep fails, and the later would benefit if the script could recognise if someone had removed / closed the screen locker.

    Is there a "universal" (within X11) way to tell if a screen locker has locked the screen?

    Thanks
    David
    I don't know about a completely 'universal' method since it might depend on the DE in use, but one approach to listen to dbus...

    https://unix.stackexchange.com/quest...-in-kde/361614
    https://superuser.com/questions/8205...creen-lock-log
    Last edited by deano_ferrari; 23-Oct-2017 at 11:45.

  6. #6
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    168

    Thumbs up Re: KDE5 switch hibernate / suspend in night / day

    Thanks for all the contributions. I've built a script which works for me, offered for others here:

    Code:
    #!/bin/sh
    # check if suspend is OK, and do it
    
    MyName="$( basename "$0" '.sh' )" || MyName="$0"
    logger --tag "$MyName" $0 $@
    
    # Get parameters
    [ -f "$HOME/.$MyName.conf" ] && . "$HOME/.$MyName.conf"
    
    # Numeric with default & minimum value
    numeric ()
    {
      if echo "$1" | egrep -q '^([1-9][0-9]*|0)$' ; then
        if echo "$3" | egrep -q '^([1-9][0-9]*|0)$' && [ $3 -gt $1 ] ; then
          echo "$2"
        else
          echo $1
        fi
      else
        echo "$2"
      fi
    }
    
    DEBUG=$( numeric "$DEBUG" 0 )
    
    [ -n "$BlockList" ] || BlockList='ssh vnc'
    [ -n "$SuspendAfter" ] && echo "$SuspendAfter" | egrep -q '^[0-9]{4}$' || SuspendAfter='0800'
    [ -n "$HibernateAfter" ] && echo "$HibernateAfter" | egrep -q '^[0-9]{4}$' || HibernateAfter='2200'
    SuspendCmd="systemctl suspend"
    HibernateCmd="systemctl hibernate"
    
    RetryInterval=$( numeric "$RetryInterval" 5 1 ) # seconds
    QuietTime=$( numeric "$QuietTime" 60 ) # seconds
    MaxTime=$( numeric "$MaxTime" 300 ) # seconds
    QuietIntervals=$(( $QuietTime / $RetryInterval +1 ))
    MaxIntervals=$(( $MaxTime / $RetryInterval +1 ))
    
    dbusPath=''
    for X in org.freedesktop.ScreenSaver org.kde.ScreenSaver org.gnome.ScreenSaver ; do
      if qdbus $X /ScreenSaver &>/dev/null ; then
        dbusPath=$X
        break
      fi
    done
    [ -z "$dbusPath" ] && dbusCmd='' || dbusCmd="qdbus $dbusPath /ScreenSaver $dbusPath"
    
    [ $DEBUG -lt 2 ] || logger --tag "$MyName" "BlockList=$BlockList"
    [ $DEBUG -lt 3 ] || logger --tag "$MyName" "SuspendAfter=$SuspendAfter HibernateAfter=$HibernateAfter"
    [ $DEBUG -lt 3 ] || logger --tag "$MyName" "RetryInterval=$RetryInterval QuietTime=$QuietTime QuietIntervals=$QuietIntervals MaxTime=$MaxTime MaxIntervals=$MaxIntervals"
    [ $DEBUG -lt 3 ] || logger --tag "$MyName" "dbusPath=$dbusPath dbusCmd=$dbusCmd"
    
    
    Retries=$QuietIntervals
    while [ $Retries -gt 0 ] ; do
      Retries=$(( $Retries - 1 ))
      MaxTime=$(( $MaxTime - 1 ))
      
      # Screensaver no longer active?
      [ $DEBUG -ge 3 ] && [ -n "$dbusCmd" ] && logger --tag "$MyName" "ScreenSaver.GetActive=$( $dbusCmd.GetActive )"
      [ -z "$dbusCmd" ] || if $dbusCmd.GetActive | egrep -qi '^[[:space:]]*false[[:space:]]*$' ; then
        [ $DEBUG -lt 1 ] || logger --tag "$MyName" "Exiting as ScreenSaver no longer active"
        exit 127
      fi
      
      for X in $BlockList ; do
        killall -s 0 --quiet "$X" && break
      done
      if [ $? -eq 0 ] ; then
        [ $DEBUG -lt 2 ] || logger --tag "$MyName" "$X still active"
        Retries=$QuietIntervals
      fi
    
      if [ $Retries -gt 0 ] ; then
        # Don't run twice
        if [ -n "$( pidof -s -x -o %PPID "$0" )" ] ; then
          logger --tag "$MyName" "Exiting as $0 is already running."
          exit 127
        fi
        
        # Don't wait too long
        if [ $MaxTime -le 0 ] ; then
          [ -z "$dbusCmd" ] || $dbusCmd.SimulateUserActivity
          exit 127
        fi
    
        [ $DEBUG -lt 3 ] || logger --tag "$MyName" "$Retries sleep $RetryInterval"
        sleep $RetryInterval
      fi
        
    done
    
    TIME="$( date '+%H%M' )"
    [ $DEBUG -lt 3 ] || logger --tag "$MyName"  "$TIME $SuspendAfter $HibernateAfter"
    if [ -n "$TIME" ] && [ -n "$SuspendAfter" ] && [ -n "$HibernateAfter" ] && [ -n "$SuspendCmd" ] && [ -n "$HibernateCmd" ] ; then
      if [ "$SuspendAfter" \< "$HibernateAfter" ] ; then
        if [ "$SuspendAfter" = "$TIME" ] || ( [ "$SuspendAfter" \< "$TIME" ] && [ "$TIME" \< "$HibernateAfter" ] ) ; then
          [ $DEBUG -lt 2 ] || logger --tag "$MyName" "$SuspendAfter le $TIME and $TIME lt $HibernateAfter"
          [ $DEBUG -lt 1 ] || logger --tag "$MyName" $SuspendCmd
          exec $SuspendCmd
        else
          [ $DEBUG -lt 2 ] || logger --tag "$MyName" "$SuspendAfter gt $TIME or $TIME ge $HibernateAfter"
          [ $DEBUG -lt 1 ] || logger --tag "$MyName" $HibernateCmd
          exec $HibernateCmd
        fi
      else
        if [ "$HibernateAfter" = "$TIME" ] || ( [ "$HibernateAfter" \< "$TIME" ] && [ "$TIME" \< "$SuspendAfter" ] ) ; then
          [ $DEBUG -lt 2 ] || logger --tag "$MyName" "$SuspendAfter gt $TIME and $TIME ge $HibernateAfter"
          [ $DEBUG -lt 1 ] || logger --tag "$MyName" $HibernateCmd
          exec $HibernateCmd
        else
          [ $DEBUG -lt 2 ] || logger --tag "$MyName" "$SuspendAfter le $TIME or $TIME lt $HibernateAfter"
          [ $DEBUG -lt 1 ] || logger --tag "$MyName" $SuspendCmd
          exec $SuspendCmd
        fi
      fi
    fi
    
    exit 0
    It, as usual comes with no warranties or guarantees. Use at your own risk!

  7. #7
    Join Date
    Jun 2008
    Location
    Auckland, NZ
    Posts
    20,286
    Blog Entries
    1

    Default Re: KDE5 switch hibernate / suspend in night / day

    Nice work. I'm sure it will be of value for others who come searching.

  8. #8
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    168

    Exclamation Re: KDE5 switch hibernate / suspend in night / day

    A word of warning about this script: it won't detect if a program (e.g. dd) is being run by another user (e.g. as root using sudo)!

    This deficiency can be fixed using additional entries in /etc/sudoers to permit killall -s 0 - [A-Za-z0-9]* (not tested) and more code in the script to detect if this is present and use it if so, but this is too much messing about for me now.

    If someone fixes this, please repost or put on githib ...
    Last edited by jetojedno; 02-Dec-2017 at 06:06. Reason: bad appearance

Tags for this Thread

Posting Permissions

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