m1gin 51

I spend so much time in front of the computer and that cause a neck pain and the eyes hurt.
I wanted to lock the screen like 15 minutes every hour to help me control myself and rest.

So I thought I can use crontab to lock the screen.

Normally in Xubuntu 19.10 I can lock the screen by the following commands:

  • dm-tool lock
  • dm-tool switch-to-greeter
  • xflock4

But when I type this command in crontab, they don't work. After spending a couple of hours, I noticed dm-tool give an error like:

Not running inside a display manager, XDG_SEAT_PATH not defined

I run printenv in terminal to see the variables and get the XDG_SEAT_PATH value and used it.

printenv | grep XDG_SEAT_PATH

Then I used crontab as follow and it worked:

45 * * * * export DISPLAY=:0 XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0; dm-tool lock

Actually instead of just typing the command that lock the screen in crontab as one line, I created a script file to do more. Like changing my password to not allow me to continue to the session before the rest time is over. :)

Let me share that part also:

#2019-11 - lock screen every hour four 15 minutes to help resting and minimize neck pain. :)

mnt=$(date +%-M); # 1, 2, 3
#mnt=$(date +%M); # 01, 02, 03

if (( $mnt == 45 )); then
#change user password to not allow enter during lock time
rn=$(echo $(( RANDOM % 100 )));
echo "$rn" > /tmp/lockpass.txt ;
echo "currentpassword" | sudo -S bash -c "echo -e '$rn\n$rn' | passwd currentuser";
export DISPLAY=:0;
​​​​​​​ export XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0;
dm-tool lock &
#xflock4 ;
#xterm -hold -display :0 -e 'dm-tool lock' &
elif (( $mnt == 0 )); then
espeak "screen lock time is over.";
#set user pass to the original one
rn=$(cat /tmp/lockpass.txt);
echo "$rn" | sudo -S bash -c "echo -e 'currentpassword\ncurrentpassword' | passwd currentuser";

Add to: