Problem: If a user is still, for example, in the login screen, xprintidle and wmctrl fails since the desktop isn't yet loaded.
In order to avoid this, I've put the next lines at the very beginning of the script:

while:
do
sleep 10s
[ -n `who | grep "$USER"` ] && break
done

So, the script waits the user (the USER variable is set to my user-name in the crontab file) is logged. But, it a user begins, for example, a terminal session (and not a graphical session like KDE or GNOME), the script also continue.

How can I determine if a user is already in a "graphical" session capable of "showing desktop mode" or not? And moreover, how can I ensure that a "graphical" session is completely loaded and not in process of loading or something like that?

My solution:
My (informal) solution is adding in the main loop the grep line:

5 Answers
5

Try to use D-Bus to query session information from logind service. It has org.freedesktop.login1.Manager interface with several signal like SessionNew and SeatNew. org.freedesktop.login1.Seat and org.freedesktop.login1.User interfaces. It can help to get Session/Seat/User state.

Use login session startup script ~/.xprofile to create some flag file for you. Be it ~/.xlogin_flag, then in your other script use inotifywatch from package inotify-tools to see it being created, touched or deleted.