How to auto-lock your computer on a schedule (e.g. at bedtime)

I’m terrible at maintaining a consistent bedtime and often stay up on the computer late into the night. Admittedly, this is usually the time when I’m the most productive. I get so into things that I lose track of time. However, this bad habit makes it hard to get enough hours of sleep; something that’s not too good for the human body. Here's how to get your computer to lock the screen at bedtime and help any of you developer-types maintain a more consistent bedtime.

iOS and Android have had some sort of “digital well-being” and time-management for about two years. These features are — among other things — designed to help you wind down at night and help you manage time and get enough sleep. It’s not a magic solution but its something that can be helpful. However, computers are lagging behind the trend and don’t offer the right tools.

I wanted my computer to kick me out of my gaming, programming, writing, or whatever activity I’m doing. I don’t want it power down or lose any ongoing work. I just wanted the screen to abruptly force me back to the login screen and out of whatever activity I’m doing.

Locking the screen doesn’t prevent me from logging back in and continuing my work. However, it’s a very strong and abrupt indicator to my brain that “hey, it’s late — go to bed!” I can admit failure and log back in. However, I feel slightly guilty about it and I more often do the adult thing and go to bed instead.

What follows are instructions for how to set up scheduled screen locking for Windows 10 [skip to Windows instructions], MacOS [skip to MacOS instructions], and Linux (systemd) [skip to Linux instructions].

By default, these instructions will lock your computer at 23:45. You can follow the optional instruction step to set a different lock-time.

Windows 10

Microsoft offers parental control features for Windows for managing the time spent on the computer. However, you can’t apply them to yourself (as a full-privilege administrative user.) There are over a dozen third-party programs for limiting screen time, but most of these are over-complicated and expensive subscription services.

You can set up screen locking on a schedule using Windows’ built-in tools, however. Task scheduling in Windows isn’t something most users will be familiar with but I’ll walk you through it.

  1. Download lock-session-bedtime.xml.
  2. [Optional] Right-click on the downloaded file and choose Edit. Modify the hour and minutes in the StartBoundary to set your own log-out time (the date just needs to be any date in the past.) Take care not to make any other changes to the file.
  3. Open the Task Scheduler program (use the search field in the Start Menu to find it.)
  4. Expand the Action menu in the menu bar and select Import task.
  5. Chose the downloaded file and click OK.

You’re all set up! You can change the scheduled time or remove the task from inside the Task Scheduler. The task will be called lock-session-bedtime. You may want to bookmark this article for future reference.

MacOS

Apple provides a built-in option that will put your Mac to sleep on a schedule. You can get the setup instructions from Apple. However, this will open a dismissable dialog with a ten-minute countdown at the time you schedule it to sleep. This isn’t what I want. It’s to easy to just click Cancel and be done with it.

MacOS has a second built-in option called downtime. Again, you can get the instructions for that from Apple. This is a more complex system where you can carve out exceptions for certain apps and micro-manage everything.

Neither of these options is what I want. In lieu of a better built-in option, I needed to create a custom scheduled task. MacOS schedules tasks through the launchd service manager. Here are instructions for how to install a launchd service that’ll log you out at 23:45 every night.

  1. Download local.lock-session-bedtime.plist.
  2. [Optional] Right-click on the downloaded file and choose Open With: Other: TextEdit. Modify the hour and minutes fields to set your own log-out time. Take care not to make any other changes to the file.
  3. Open Finder. Expand the Go menu in the top bar and select Library. If you don’t see Library in the menu, select Go To Folder, and type ~/Library and press Enter.
  4. Open the LaunchAgents folder inside your Library folder (or create it if it doesn’t exist.)
  5. Move the downloaded file into that folder. You can use drag-and-drop from the Downloads folder or copy-and-paste.
  6. Log out and back in again (or restart your Mac) from the Apple menu in the top left corner.

That’s it. You’re all set. You can remove or modify the scheduled time in the file later to undo or change the behavior. You may want to bookmark this article for future reference.

Linux (systemd)

The following method should work on terminal sessions and graphical sessions (including Plasma and GNOME) on any system managed by systemd (almost all modern Linux distributions.)

  1. Download lock-session-bedtime.service and lock-session-bedtime.timer.
  2. [Optional] Edit the downloaded timer file and modify it to set your own log-out time. Take care not to make any other changes to the file.
  3. Move the downloaded files into the ~/.config/systemd/user/ directory inside your Home folder (or create the folder if it doesn’t exist.)
  4. Open a terminal shell and type the following two commands:
systemctl --user daemon-reload

systemctl --user enable --now \
        lock-session-bedtime.timer

You’re all set up! You can change the scheduled time by modifying the timer file or remove it by deleting the timer and service files. You can review the service file if you’re curious about how this works under the hood.