Sun-dried tomatoes' sundry thoughts

Wednesday, March 18, 2026

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.

Bash
#!/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.

Bash
sudo nano /usr/local/bin/samba_inhibit_check.sh
  • Paste the code above into the editor.

  • Press Ctrl+O, then Enter to save.

  • Press Ctrl+X to exit.

Step 2: Make it Executable

The system needs permission to actually "run" the file.

Bash
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.

Bash
sudo crontab -e
  • If it asks, choose nano as your editor.

  • Scroll to the very bottom and add this line:

Code snippet
*/5 * * * * /usr/local/bin/samba_inhibit_check.sh
  • Save and exit (Ctrl+O, Enter, Ctrl+X).


🔍 How the Logic Flows

  1. The Watchdog: Every 5 minutes, Cron wakes up the script.

  2. The Interrogation: The script asks Samba, "Is anyone locking a file?"

  3. The Decision:

    • Yes? It starts a background systemd-inhibit process. 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:

Bash
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