Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions terminawake
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
#!/bin/bash

# Check if the script is run as root
# EUID is an environment variable that represents the effective user ID of the current user
# The root user has an EUID of 0, so we're checking if the EUID is not 0
# If the script is not run as root, print an error message and exit with a non-zero status code
if [[ $EUID -ne 0 ]]; then
echo "This script must be run with root privileges (sudo)."
exit 1
fi

# Function to reenable sleep and exit the script
# This function is called when the script needs to exit, to ensure that sleep is reenabled before exiting
function quit {
# Reenable sleep for battery and AC power modes
pmset -b disablesleep 0
pmset -c disablesleep 0
sudo pmset -b disablesleep 0
sudo pmset -c disablesleep 0

# Exit the script
exit
}

# Prevent sleep indefinitely for battery and AC power modes
pmset -b disablesleep 1
pmset -c disablesleep 1
sudo pmset -b disablesleep 1
sudo pmset -c disablesleep 1

# Calculate the total duration in seconds from all arguments
total_duration_sec=0
Expand All @@ -38,7 +29,7 @@ for arg in "$@"; do
h) duration_sec=$((value * 3600)) ;;
*) echo "Invalid argument: $arg"; exit 1 ;; # Exit with error message if invalid argument is provided
esac

# Add the duration in seconds to the total duration
total_duration_sec=$((total_duration_sec + duration_sec))
done
Expand All @@ -61,7 +52,7 @@ else
sleep_pid=$!
# Remove this job from the shell's active list to avoid getting a termination message
disown $sleep_pid

# Wait for user to press Enter or for the sleep duration to pass
# If the user presses Enter before the duration is over, kill the sleep command
if ! read -t "$total_duration_sec" -r; then
Expand Down