LInuxMint sleep Script to check for active file edits
The Final Script: samba_inhibit_check.sh
This script ensures your PC stays awake only when someone is actually transferring or editing files.
#!/bin/bash
# Path to the file that tracks our "Stay Awake" process
PID_FILE="/tmp/samba_inhibit.pid"
# 1. Check for active SMB file locks (The "Is anyone actually doing something?" check)
# We count 'DENY_WRITE' locks because they represent active file handles.
ACTIVE_LOCKS=$(/usr/bin/smbstatus -S | grep -c 'DENY_WRITE')
if [ "$ACTIVE_LOCKS" -gt "0" ]; then
# --- ACTIVITY DETECTED ---
# Check if we are already holding the system awake
if [ ! -f "$PID_FILE" ] || ! ps -p "$(cat "$PID_FILE")" > /dev/null; then
echo "Active SMB locks found ($ACTIVE_LOCKS). Inhibiting sleep..."
# This command creates a "lock" that tells Mint's power manager NOT to sleep.
/usr/bin/systemd-inhibit --what=sleep --who="SambaMonitor" --why="Active SMB file transfer" sleep infinity &
# Save the ID of that lock process so we can stop it later
echo $! > "$PID_FILE"
else
echo "Activity continues. Sleep is already inhibited."
fi
else
# --- IDLE ---
# If a lock exists from a previous check, kill it to allow the PC to sleep.
if [ -f "$PID_FILE" ]; then
echo "No active locks. Releasing sleep inhibition."
kill "$(cat "$PID_FILE")" 2>/dev/null
rm -f "$PID_FILE"
fi
echo "System is idle. Normal power management applies."
fi
exit 0
⚙️ How to Install and Configure
Follow these steps in your terminal to set it up:
Step 1: Create the Script File
We’ll put it in /usr/local/bin so it's safe and system-wide.
sudo nano /usr/local/bin/samba_inhibit_check.sh
Paste the code above into the editor.
Press
Ctrl+O, thenEnterto save.Press
Ctrl+Xto exit.
Step 2: Make it Executable
The system needs permission to actually "run" the file.
sudo chmod +x /usr/local/bin/samba_inhibit_check.sh
Step 3: Schedule it with Cron
We want this to run every 5 minutes to keep an eye on things.
sudo crontab -e
If it asks, choose
nanoas your editor.Scroll to the very bottom and add this line:
*/5 * * * * /usr/local/bin/samba_inhibit_check.sh
Save and exit (
Ctrl+O,Enter,Ctrl+X).
🔍 How the Logic Flows
The Watchdog: Every 5 minutes, Cron wakes up the script.
The Interrogation: The script asks Samba, "Is anyone locking a file?"
The Decision:
Yes? It starts a background
systemd-inhibitprocess. Your PC is now basically a bouncer for its own power button—it won't let "Sleep" into the club.No? It kills that background process. Now, Linux Mint’s standard power settings (e.g., "Sleep after 20 minutes of inactivity") can take over normally.
🧪 How to Verify it's Working
To see if your script has successfully blocked sleep while you're using a file, run:
systemd-inhibit --list
Look for "SambaMonitor" in the list. If it's there, your script is doing its job!

0 Comments:
Post a Comment
<< Home