In today’s tech-driven world, conserving power on our devices is more important than ever. If you’re often away from your computer and want to automate putting it into a suspended state, then a simple Bash script can help. This Sleep Timer Bash Script allows users to set a timer in minutes, after which the system will suspend, saving energy and resources.
Prerequisites
- A basic understanding of Bash scripting
- Familiarity with
systemctl
commands - A Linux-based operating system
- systemd must be installed on your system
DID YOU KNOW?
System suspension helps in lowering power consumption significantly, which is especially beneficial for laptops to extend battery life.
The Script
This script provides a straightforward way to implement a sleep timer. It begins by validating the user’s input to ensure that it is indeed a positive integer representing the desired delay time in minutes. The timer is then converted to seconds for processing. The user is informed of the countdown duration and can cancel the process using Ctrl+C
. After the countdown, the script executes the systemctl suspend -i
command, forcing the system to suspend regardless of active sessions.
#!/bin/bash
# Prompt for time in minutes and validate input
read -p "Enter time in minutes: " input_time
if ! [[ "$input_time" =~ ^[1-9][0-9]*$ ]]; then
echo "Please enter a positive integer."
exit 1
fi
# Convert minutes to seconds
let "sleep_time=input_time*60"
echo "System will suspend in $input_time minutes. Press Ctrl+C to cancel."
# Timer loop
sleep $sleep_time
# Suspend the system
systemctl suspend -i
Step-by-Step Explanation
NOTE!
Make sure you have the necessary permissions to execute the suspend command.
The following steps break down the script for better understanding:
- Input Validation: The script prompts the user for input and checks if the entered value is a positive integer.
- Time Conversion: The input time in minutes is converted into seconds for accurate countdown.
- Countdown Notification: The user is informed of the countdown duration, allowing cancellation if needed.
- System Suspension: After the timer completes, the system is suspended using the
systemctl suspend -i
command.
How to Run the Script
To execute this script, follow these simple steps:
- Open your terminal.
- Copy the script into a new file, for example,
sleep_timer.sh
. - Make the script executable by running
chmod +x sleep_timer.sh
. - Run the script using
./sleep_timer.sh
.
Conclusion
This Sleep Timer Bash Script is a practical tool for automating system suspension and conserving energy during inactive periods. By understanding the script’s components and how to implement it, you can efficiently manage your system’s power usage.
FAQ
-
What if I enter an invalid time?
The script will prompt you to enter a positive integer, preventing execution with invalid input.
-
Can I modify the timer duration after starting the countdown?
No, once the countdown has begun, it cannot be modified. You can only cancel it with
Ctrl+C
. -
Does the script require root permissions?
The script may require root permissions to execute the
systemctl suspend -i
command, depending on your system’s configuration. -
Will this work on all Linux distributions?
Yes, as long as the distribution uses systemd, this script should work without issues.
Troubleshooting
If you encounter issues while running the script, consider the following common problems:
- Error: Permission denied when trying to execute the script.
Ensure you have made the script executable withchmod +x sleep_timer.sh
. - Error: System won’t suspend.
Check if you have the necessary permissions to usesystemctl suspend
. - Warning: The system is in use, and suspension may not be allowed.
Ensure all applications are closed or the system is idle before running the script.