We’ve all been there. You’re deep in the zone, juggling three different laptops like some kind of tech wizard, only to glance over at your Arch machine and see… a black screen.
Nothing kills a workflow faster than having to wait for a laptop to wake up and re-authenticate your login just because you didn’t touch the keyboard for ten minutes. If you’re running KDE Plasma on Wayland, you know that “Energy Saving” settings can be a bit of a blunt instrument. You either keep it on all the time (bad for your battery/screen) or let it sleep (bad for your sanity).
Here is a quick, “set it and forget it” way to keep your Arch system awake strictly during working hours.
The Magic Script: prevent-sleep.sh
We’re going to use a bash script that checks two things: the time and the power source. If it’s between 9 AM and 5 PM and you’re plugged into a charger, the script tells the system, “Hey, don’t sleep.”
First, create the file: sudo nano /usr/local/bin/prevent-sleep.sh
Paste this in:
Bash
#!/bin/bash
START_HOUR=9
END_HOUR=17
while true; do
CURRENT_HOUR=$(date +%H)
# Check if the AC adapter is plugged in (1 = yes, 0 = no)
ON_AC=$(cat /sys/class/power_supply/AC/online)
if [ "$CURRENT_HOUR" -ge "$START_HOUR" ] && [ "$CURRENT_HOUR" -lt "$END_HOUR" ] && [ "$ON_AC" -eq 1 ]; then
# This is the heavy lifter. It inhibits sleep for 60 seconds.
systemd-inhibit --what=sleep:idle --who="TimeScript" --why="Scheduled active hours" sleep 60
else
# Outside hours or on battery? Just wait.
sleep 60
fi
done
Why this works:
systemd-inhibit: This is a powerful utility that tells the system manager to ignore idle requests.- The AC Check: We’ve included a check for
$ON_AC. This ensures that if you take your laptop to a coffee shop and forget to plug it in, it will still sleep so your battery doesn’t die in your bag.
Permissions: Giving the Green Light
Since we put this in /usr/local/bin, we need to make sure you have the right to run it. Run these two commands:
- Make it executable:
sudo chmod +x /usr/local/bin/prevent-sleep.sh - Change ownership to yourself: (Replace
yourusernamewith your actual username)sudo chown yourusername:yourusername /usr/local/bin/prevent-sleep.sh
Making it Automatic (The KDE Way)
Since we’re on KDE, we don’t need to mess with complex terminal configurations to get this started.
- Open System Settings.
- Navigate to Startup and Shutdown -> Autostart.
- Click + Add… -> Add Application… (or Add Login Script).
- Point it to
/usr/local/bin/prevent-sleep.sh.
Now, every time you log in, your silent guardian starts running in the background.
Pro Tip: If you want to go “full Linux nerd,” you could also set this up as a Cron Job using
crontab -eor create a Systemd User Service. But for most of us, the KDE Autostart is the path of least resistance.
How to check if it’s actually working
Not sure if the script is doing its job? You don’t have to sit and stare at your screen for 10 minutes to find out. Open your terminal and run:
systemd-inhibit --list
This command shows you every process currently “holding the lock” on your system’s power management. If you see “TimeScript” in that list, you’re golden! Your laptop will stay awake and ready to work as long as the sun is up and the charger is in.
Wait—A Quick Reality Check on Security Before you go full “never sleep” mode, keep in mind that an awake laptop is an unlocked laptop. If you’re working in a public cafe, a shared co-working space, or a busy office where people can walk by your desk, do not use this script. Preventing your system from idling means anyone can access your data the moment you step away for a coffee. This setup is strictly for a secure, private home office environment where you have total control over who is in the room. Safety first, convenience second!