Sunday, June 15, 2025

Getting rtcwake working on Bluefin

#!/bin/bash
# User systemd setup for auto suspend/wake on Bluefin

#Please run these two commands manually:

#1. First, create the sudo rule:
echo "$(whoami) ALL=(ALL) NOPASSWD: /usr/bin/rtcwake" | sudo tee /etc/sudoers.d/rtcwake-nopasswd
#bash
#2. Then set the correct permissions:
sudo chmod 440 /etc/sudoers.d/rtcwake-nopasswd
#bash
#After you've run those commands, we can test if passwordless sudo works:
sudo rtcwake -m show
#❯ sudo rtcwake -m show
#alarm: off

# Create user service directory

mkdir -p ~/.config/systemd/user

# Create helper script for suspend + rtcwake
mkdir -p ~/bin
cat > ~/bin/rtc-suspend.sh << 'EOF'
#!/bin/bash
# Helper script to suspend and set RTC wake time
# Usage: rtc-suspend.sh "wake_time_description"

if [ $# -ne 1 ]; then
echo "Usage: $0 'wake_time_description'"
echo "Example: $0 '15:00 today'"
exit 1
fi

wake_time="$1"
wake_timestamp=$(date -d "$wake_time" +%s)

if [ $? -ne 0 ]; then
echo "Error: Invalid time format '$wake_time'"
exit 1
fi

echo "Setting RTC wake for: $(date -d @$wake_timestamp)"

# Set the RTC wake time first (before suspend)
sudo rtcwake -m no -t $wake_timestamp

if [ $? -eq 0 ]; then
echo "RTC wake time set successfully"
# Small delay then suspend
sleep 2
systemctl suspend
else
echo "Error: Failed to set RTC wake time"
exit 1
fi
EOF

chmod +x ~/bin/rtc-suspend.sh

# Create the morning suspend service (9 AM - 3 PM)
cat > ~/.config/systemd/user/morning-suspend.service << 'EOF'
[Unit]
Description=Suspend system at 9 AM until 3 PM
After=graphical-session.target

[Service]
Type=oneshot
Environment=PATH=/usr/local/bin:/usr/bin:/bin:%h/bin
ExecStart=%h/bin/rtc-suspend.sh "15:00 today"
Restart=no

[Install]
WantedBy=default.target
EOF

# Create the evening suspend service (9 PM - 5 AM)
cat > ~/.config/systemd/user/evening-suspend.service << 'EOF'
[Unit]
Description=Suspend system at 9 PM until 5 AM tomorrow
After=graphical-session.target

[Service]
Type=oneshot
Environment=PATH=/usr/local/bin:/usr/bin:/bin:%h/bin
ExecStart=%h/bin/rtc-suspend.sh "05:00 tomorrow"
Restart=no

[Install]
WantedBy=default.target
EOF

# Create timer for 9 AM suspend
cat > ~/.config/systemd/user/morning-suspend.timer << 'EOF'
[Unit]
Description=Trigger morning suspend at 9 AM
Requires=morning-suspend.service

[Timer]
OnCalendar=*-*-* 09:00:00
Persistent=true
AccuracySec=1min

[Install]
WantedBy=timers.target
EOF

# Create timer for 9 PM suspend
cat > ~/.config/systemd/user/evening-suspend.timer << 'EOF'
[Unit]
Description=Trigger evening suspend at 9 PM
Requires=evening-suspend.service

[Timer]
OnCalendar=*-*-* 21:00:00
Persistent=true
AccuracySec=1min

[Install]
WantedBy=timers.target
EOF

echo "Setting up user systemd services..."

# Reload user systemd daemon
systemctl --user daemon-reload

# Enable the services
systemctl --user enable morning-suspend.timer
systemctl --user enable evening-suspend.timer
#systemctl --user enable wake-notification.service

# Start the timers
systemctl --user start morning-suspend.timer
systemctl --user start evening-suspend.timer

# Enable lingering so services run when not logged in
sudo loginctl enable-linger $USER

echo ""
echo "=== SETUP COMPLETE ==="
echo ""
echo "Your system will now:"
echo " • Suspend at 9:00 AM and wake at 3:00 PM"
echo " • Suspend at 9:00 PM and wake at 5:00 AM"
echo ""
echo "Active hours:"
echo " • 5:00 AM - 9:00 AM (4 hours)"
echo " • 3:00 PM - 9:00 PM (6 hours)"
echo ""
echo "=== TESTING ==="
echo "Test your RTC wake capability first:"
echo " sudo rtcwake -m mem -s 30 # 30-second test"
echo ""
echo "Test the helper script:"
echo " ~/bin/rtc-suspend.sh '$(date -d '+2 minutes' +'%H:%M today')'"
echo ""
echo "=== MONITORING ==="
echo "Check timer status:"
echo " systemctl --user list-timers"
echo ""
echo "View service logs:"
echo " journalctl --user -u morning-suspend.service"
echo " journalctl --user -u evening-suspend.service"
echo ""
echo "=== CONTROL ==="
echo "Disable (stop automatic suspend):"
echo " systemctl --user stop morning-suspend.timer evening-suspend.timer"
echo " systemctl --user disable morning-suspend.timer evening-suspend.timer"
echo ""
echo "Re-enable:"
echo " systemctl --user enable morning-suspend.timer evening-suspend.timer"
echo " systemctl --user start morning-suspend.timer evening-suspend.timer"
echo ""
echo "=== TROUBLESHOOTING ==="
echo "If suspend doesn't work, check:"
echo " 1. Your user can suspend: 'systemctl suspend'"
echo " 2. RTC wake works: 'sudo rtcwake -m mem -s 30'"
echo " 3. User lingering is enabled: 'loginctl show-user $USER'"
echo ""
echo "Common issues on Bluefin:"
echo " • May need to add user to 'wheel' group for suspend permissions"
echo " • Some hardware doesn't support RTC wake from suspend"
echo " • Check BIOS/UEFI settings for wake-on-RTC support"



No comments:

Post a Comment

Getting rtcwake working on Bluefin

#!/bin/bash # User systemd setup for auto suspend/wake on Bluefin #Please run these two commands manually: #1. First, create the sudo rule: ...