Is it possible to run something (e.g. a script) after a certain idle time?

Kai-Martin Knaak knaak at iqo.uni-hannover.de
Sun Oct 27 22:12:28 CET 2019


On 21. October 2019 Chris Green <cl at isbd.net> wrote:

> I have my system set up so that the Power Manager turns off the
> display after about 15 minutes idle.  Is it possible to trigger
> running a script in the same way?  I have some actions I'd like taken
> at the same time (or similar) to when the display turns off.

I maintain a bunch of semi public Desktops that enforce logout from
the graphical XFCE session. After 30 minutes idle time a dialogue is presented
with a countdown time of yet another hour. If the user fails to click
on the dialogue during this grace period an automatic logout is
initiated.

This is accomplished by a process that hooks into the screensaver mechanics. 
That way I don't have to mess with low level dbus and stuff. It requires a 
desktop entry and the script that presents the dialogue and triggers the 
actual logout:

---------------------8<----------------------
# This desktop entry is part of the automatic logout function of the computers in
# in the ElektronIQ. Clients link to this file. It starts xautolock in the background.
###########################################-<)kmk(>-2016
[Desktop Entry]
Type=Application
Name=ElektronIQ auto log out
Comment=automatically log out user after a period of inactivity and a grace time
TryExec=xautolock -time 30 -locker "/usr/local/bin/elektroniq-logout-countdown.sh
Exec=xautolock -time 30 -locker "/usr/local/bin/elektroniq-logout-countdown.sh
Icon=utilities-system-monitor.svg
OnlyShowIn=XFCE;
Terminal=false
Hidden=true
-------------------->8-----------------------

--------------------8<-----------------------
#!/bin/sh
# Logout script for the computers at ElectronIQ
# * supposed to be launched by xautolock
# * presents a count-down progress bar with zenity
# * initiates a logout via the command xfce4-session-logout
# * on logout an email is sent to the user and to an admin
# * if the user presses "cancel" or closes the dialog the session continues
#
# This script is supposed to be called by xautolock:
#    xautolock -time 60 -locker "elektronIQ-logout-countdown.sh"
#############################################-<)kmk(>-2016##################

GRACE=3600                      # grace period in seconds
GRACEMINUTES=$(($GRACE / 60))   # grace period in minutes

(
 SECONDS=$GRACE
 while [ $SECONDS -ge 0 ]
   do
     PERCENT=$((100 * $SECONDS / $GRACE ))
     if [ $PERCENT -lt 100 ]       # do not report "100" because this
       then                        # would make the progress bar close
         echo $PERCENT             # immediately.
       fi
     echo "# Automatic logout in "$(($SECONDS / 60))" minutes and "$(($SECONDS % 60))" seconds"
     SECONDS=$(($SECONDS-1))
     sleep 1
   done
) |           # pipe count-down to zenity
zenity --width=450 --progress \
       --title="Auto-Logout" \
       --text="Automatic logout..." \
       --percentage=100 \
       --auto-close

RESULT=$?
# timer got to zero --> RESULT = 0
# user pressed "cancel" --> RESULT = 1
# dialog was closed by user --> RESULT = 1

if [ $RESULT = "0" ]
 then
  echo "Timer expired --> terminate the XFCE session"

  DATE=$(date +%F)
  TIME=$(date +%R)
  HOST=$(hostname)
  EMAIL=$(ldapsearch -ZZ uid=$USER mail 2>/dev/null | awk -F ': ' '$1=="mail"{print $2}')
  ADMIN="knaak at iqo.uni-hannover.de"

  # Send an email to the user
  mail -t <<EOF
TO: $EMAIL
FROM: root@$HOST.iqo.uni-hannover.de
Reply-To: $ADMIN
SUBJECT: automatic logout from $HOST

Hello $USER.
On $DATE, $TIME you have been automatically logged out from the computer $HOST
at the electronics workshop. Automatic logout is initiated after half an hour
of inactivity and $GRACEMINUTES minutes grace period.
You can drop $ADMIN an email if you need a longer grace period.

Greetings from the faithful logout script.
EOF

  # Send an email to the admin
  mail -t <<EOF
TO: $ADMIN
FROM: root@$HOST.iqo.uni-hannover.de
SUBJECT: automatic logout of $USER from $HOST

On $DATE, $TIME $USER was automatically logged out from $HOST.

Greetings from the faithful logout script.
EOF

  # do the actual logout
  xfce4-session-logout --logout
 fi
exit
---------------------->8--------------------

Hope that helps, 

---<)kaimartin(>---

-- 
Kai-Martin Knaak
knaak at iqo.uni-hannover.de Universit├Ąt Hannover, Inst. f.
Quantenoptik        tel: +49-511-762-2895 Welfengarten 1, 30167
Hannover                     fax: +49-511-762-2211
https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xC13AA4CC7B0F9882
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://mail.xfce.org/pipermail/xfce/attachments/20191027/428edc26/attachment.sig>


More information about the Xfce mailing list