The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 172β€…    Votes:  1β€…    βœ… Solution
Tags: 20.04   gnome   cron   lock-screen  
Link: πŸ” See Original Answer on Ask Ubuntu πŸ”—

URL: https://askubuntu.com/q/1393684
Title: Unable to lock screen with Cron or Settings
ID: /2022/02/18/Unable-to-lock-screen-with-Cron-or-Settings
Created: February 18, 2022    Edited:  February 21, 2022
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


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.

⇧ Frequent switches between Ethernet and Wifi Convert Stack Exchange posts to your own website  β‡©