20 20 20 rule notification Ubuntu

2 min read Original article ↗

@kwkr

  • Save kwkr/b6376b4ade4d14467334bc0dbb845a16 to your computer and use it in GitHub Desktop.
Save kwkr/b6376b4ade4d14467334bc0dbb845a16 to your computer and use it in GitHub Desktop.

20 20 20 rule notification Ubuntu

#!/bin/bash
# 1. Define Paths
BIN_DIR="$HOME/.local/bin"
SCRIPT_PATH="$BIN_DIR/eyes"
PAUSE_FILE="$HOME/.eyes-paused"
mkdir -p "$BIN_DIR"
# 2. Create the notification script
cat << 'EOF' > "$SCRIPT_PATH"
#!/bin/bash
PAUSE_FILE="$HOME/.eyes-paused"
if [ -f "$PAUSE_FILE" ]; then exit 0; fi
# Environmental variables for Cron to talk to your Desktop
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
/usr/bin/notify-send -u critical -i timer "Eye Break" "20-20-20 Rule: Look 20ft away for 20 seconds!"
EOF
# 3. Make it executable
chmod +x "$SCRIPT_PATH"
# 4. Add aliases to .bashrc if they don't exist
if ! grep -q "alias eyepause" "$HOME/.bashrc"; then
echo "" >> "$HOME/.bashrc"
echo "# 20-20-20 Rule Controls" >> "$HOME/.bashrc"
echo "alias eyepause='touch $PAUSE_FILE && echo \"Eye rule paused.\"' " >> "$HOME/.bashrc"
echo "alias eyeresume='rm -f $PAUSE_FILE && echo \"Eye rule resumed.\"' " >> "$HOME/.bashrc"
echo "alias eyestatus='[ -f $PAUSE_FILE ] && echo \"Paused\" || echo \"Running\"' " >> "$HOME/.bashrc"
fi
# 5. Setup Cron Job (Runs every 20 minutes)
# We use a temp file to avoid duplicating entries if you run this setup twice
crontab -l 2>/dev/null | grep -v "$SCRIPT_PATH" > mycron
echo "*/20 * * * * $SCRIPT_PATH" >> mycron
crontab mycron
rm mycron
echo "-------------------------------------------------------"
echo "Setup Complete!"
echo "1. Command created at: $SCRIPT_PATH"
echo "2. Cron job installed (running every 20 mins)."
echo "3. Aliases 'eyepause' and 'eyeresume' added to .bashrc."
echo "-------------------------------------------------------"
echo "Run 'source ~/.bashrc' now to activate the pause commands."