0

I want to setup a cron job to lock my session every day at three PM. When I enter dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock in a terminal window, the session locks immediately, but when I do the same in crontab (even by using a BASH script) it didn't work. I want to create a BASH script to lock the session using dbus-send and add it to crontab.

PenguinCSC
  • 1,426
  • 2
  • 14
  • 26
  • I think the problem is In the terminal you are locking your screen. In Cron or systemd you are locking root's screen. – WinEunuuchs2Unix Aug 04 '20 at 11:19
  • 4
    Does this answer your question? [How can I show notify-send messages triggered by crontab?](https://askubuntu.com/questions/978382/how-can-i-show-notify-send-messages-triggered-by-crontab) More explanations are available at https://github.com/pa4080/cron-gui-launcher – pa4080 Aug 04 '20 at 11:33

1 Answers1

0

Using loginctl to lock your desktop screen from crontab.

/opt/bin/lock-session.sh:

#!/bin/bash

# crontab -e
# min hour dom month dow command
# 0 15 * * * /bin/bash -c /opt/bin/lock-session.sh

# Display     (is the first value of Sessions).
# Sessions    (array of sessions).
property=Sessions

for session in $(loginctl show-user $USER -p $property --value); do
    [[ \
    $(loginctl show-session $session -p Desktop --value) ]] && loginctl lock-session $session
done

exit 0