Bash Script to Suspend the System after User Inactivity

The modern workspace often demands hours of productivity from users, but what happens when you step away from your computer? To help manage power consumption and increase security, it’s useful to create a Bash script that automatically suspends the system after a period of user inactivity. This article will guide you through the prerequisites needed, the script itself, and how to implement it effectively.

Prerequisites

  • Basic knowledge of Bash scripting
  • Familiarity with command-line interfaces
  • Understanding of how to work with system processes
  • Install the xprintidle package on your system
  • Ensure that you have the necessary permissions to suspend the system

DID YOU KNOW?

Automating system tasks can significantly improve not just security but also energy efficiency in your workspace.

The Script

This Bash script utilizes the xprintidle command to check how long the user has been idle. If the idle time exceeds a specified threshold, it will suspend the system. Here’s the script:

#!/bin/bash

IDLE_TIME=300000  # 5 minutes in milliseconds

while true; do
    idle=$(xprintidle)
    if [ "$idle" -gt "$IDLE_TIME" ]; then
        systemctl suspend
    fi
    sleep 60  # Check every minute
done

Step-by-Step Explanation

NOTE!

This script should run in the background for continuous monitoring after you set it up.

The following steps outline how the script operates:

  1. Set Idle Time: The variable IDLE_TIME is set to 300000 milliseconds, representing the threshold for user inactivity.
  2. Infinite Loop: The script runs an infinite loop, continuously checking the idle time.
  3. Check Idle Time: It retrieves the idle time using xprintidle and compares it against the threshold.
  4. Suspending: If the idle time exceeds the specified threshold, the system will suspend.
  5. Pause for Next Check: The script waits for one minute before checking the idle time again.

How to Run the Script

Once you have the script saved on your system, follow these instructions to run it:

  1. Open your terminal.
  2. Navigate to the directory where the script is saved.
  3. Run the script using the command bash script_name.sh.

Conclusion

Creating a Bash script to suspend your system after a period of inactivity not only helps manage energy consumption but also adds a layer of security to your workplace. With just a little bit of scripting knowledge and the right tools, you can implement this feature easily and effectively.

FAQ

  1. What is xprintidle?

    xprintidle is a command-line tool that returns the number of milliseconds since the last user activity.

  2. How do I install xprintidle?

    You can install xprintidle using your package manager. For Debian-based systems, use sudo apt install xprintidle.

  3. Can I change the idle time threshold?

    Yes, simply modify the IDLE_TIME variable in the script to suit your preferences.

  4. What permissions are needed to suspend the system?

    The user running the script typically requires either superuser privileges or to belong to a group with permission to suspend the system.

  5. Is it safe to run this script at all times?

    While generally safe, ensure that no important tasks are running that would be disrupted by the system suspension.

Troubleshooting

If you encounter issues with the script, consider the following common errors and solutions:

  • Error: xprintidle not found. Solution: Ensure that xprintidle is installed on your system.
  • Error: Permission denied on suspend. Solution: Check user permissions or run the script as superuser.
  • Error: Script not executing. Solution: Ensure the script is marked as executable with chmod +x script_name.sh.