Score:1

Unable to lock screen with Cron or Settings

id flag

Has anyone been able to use crontab to lock their screen automatically? I have tried all the suggested methods here but none seems to work. I have an Ubuntu 20.04 gnome desktop and even the Screen Lock in the settings does not work. Only the following command works via the terminal:

gnome-screensaver-command -l

Here is the current code block accessed by sudo crontab -e:

SHELL=/bin/bash

0 */1 * * * systemctl restart network-manager

0 */1 * * * systemctl restart teamviewerd

*/2 * * * * gnome-screensaver-command -l
pa4080 avatar
cn flag
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)
Score:1
in flag

A more universal way of locking the screen is using:

loginctl lock-session

Cron doesn't know the Session ID for loginctl

When you use loginctl lock-session from the command line, your session ID is already known. For example:

$ echo $XDG_SESSION_ID

c2

$ loginctl list-sessions

   SESSION        UID USER             SEAT            
        c2       1000 rick             seat0           

1 sessions listed.

The above shows two ways you can get your Session ID from the command line.

This GitHub issue for loginctl explains why the variable XDG_SESSION_ID is unknown to systemd. The same case would apply to cron:

So from cron you could use loginctl lock-session c2 if you knew your session ID would always be c2. An easier way is to use lock-sessions.

I tried this on my system and it works. Try using this on your crontab -e:

*/5  *   *  *   *     loginctl lock-sessions

Now, every five minutes your screen will lock.

simguy avatar
id flag
Hey, thanks for the quick reply. This command also works from the terminal but not with `crontab`.
WinEunuuchs2Unix avatar
in flag
@simguy I've revised the answer.
simguy avatar
id flag
Thanks, but this also has the same issue. I have also added some more details to my question. Here is how I implemented it for testing: "*/2 * * * * export DISPLAY=:0 && /bin/loginctl lock-session"
WinEunuuchs2Unix avatar
in flag
@simguy Check for errors using `journalctl -xe`. One could show up every two minutes.
WinEunuuchs2Unix avatar
in flag
@simguy I've revised the answer after checking error messages and researching. If it works for you, remember to check the grey check mark next to the answer to show it solves the problem.
simguy avatar
id flag
yes, it does! Thank you very much; I will mark it as the solution.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.