Below is a simple bash script I developed which checks to see whether a user is using the X Window System before attempting to do something. Basically it checks to see if the system is idle. This can be useful if you want to run a backup or a virus scan without impacting a user that may interacting with the desktop environment.
In this example we will consider the system idle if no user interaction with the X Window System has occurred for 10 minutes. Afterwards the system will shut itself down (ideal following a backup)
This script makes uses the program xprintidle to determine how many milliseconds has passed since the user has interacted with X. It can be installed on a Debian based system using the command sudo apt-get install xprintidle
#!/bin/bash #Check if X is idle. Timer set to 10 minutes. export DISPLAY=:0 idletime=0 while [ $idletime -le 600000 ] do echo "Idle Time is: $idletime milliseconds" sleep 1 idletime=$(xprintidle) done sudo shutdown -h now