The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Auto-lock your device via script

To auto-lock your device using a script, you can write different scripts depending on the operating system you’re using. Below are examples for Windows, macOS, and Linux.

1. Windows

For Windows, you can create a script to lock your computer using the built-in rundll32 command.

  • Open Notepad and add the following line:

batch
rundll32.exe user32.dll,LockWorkStation
  • Save the file with a .bat extension, for example, lock.bat.

  • Now, running this .bat file will lock your Windows computer.

You can also schedule this to run at a specific time or after a certain delay using Task Scheduler.

2. macOS

On macOS, you can use a simple AppleScript to lock the screen.

  • Open the Script Editor app on your Mac.

  • Paste the following AppleScript:

applescript
tell application "System Events" keystroke "q" using {command down, control down} end tell
  • Save the script as an application (e.g., lock.app).

  • When you run this app, it will lock your macOS system.

Alternatively, you can run a terminal command to lock the screen:

bash
pmset displaysleepnow

This will immediately lock your screen by putting the display to sleep.

3. Linux

For Linux, the method depends on the desktop environment you’re using, but a common command is gnome-screensaver-command.

For example, on Ubuntu (GNOME desktop):

bash
gnome-screensaver-command -l

This will lock the screen immediately. You can add this command into a shell script:

bash
#!/bin/bash gnome-screensaver-command -l
  • Save the file as lock.sh, then make it executable:

bash
chmod +x lock.sh

Now, running lock.sh will lock your Linux system.

Automating with Timed Lock (Optional)

You can also schedule the script to run at specific intervals using task scheduling tools:

  • Windows: Use Task Scheduler.

  • macOS: Use launchd or cron jobs.

  • Linux: Use cron jobs.

If you’d like further help setting up a scheduled task, feel free to ask!

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About