Skip to content

Instantly share code, notes, and snippets.

@milad-rasouli
Created October 21, 2024 09:39
Show Gist options
  • Select an option

  • Save milad-rasouli/3446344a624e463c656edfe415c484be to your computer and use it in GitHub Desktop.

Select an option

Save milad-rasouli/3446344a624e463c656edfe415c484be to your computer and use it in GitHub Desktop.
Eye Reminder

Eye Reminder Script (20/20/20 Rule)

Important

Follow the 20/20/20 Rule
Every 20 minutes of screen time, look at something 20 feet away for at least 20 seconds to reduce eye strain.

Bash Script (notify.sh)

#!/bin/sh

while true; do
    echo -e "\a"  # ASCII bell character for beep sound

    # Show the message for 1 minute with a countdown timer and reminder text
    (
    for i in $(seq 1 60); do
        echo "# $((60 - i)) seconds remaining"
        echo "$((i * 100 / 60))"  # This updates the progress percentage
        sleep 1
    done
    ) | zenity --progress --title="Take a 20-second break and look 20 feet away!" --percentage=0 --auto-close &

    # Wait a moment to ensure the Zenity window is open
    sleep 1
    echo -e "\a"  

    # Wait for 19 minutes (1140 seconds) before showing the message again
    sleep 1140
done

Note

Notification Message
The script uses Zenity to display a pop-up message reminding you to follow the 20/20/20 rule:
"Take a 20-second break and look 20 feet away!"


Instructions for Ubuntu

1. Install Zenity

Ensure Zenity is installed, as it is required to display the GUI pop-up.

  • Ubuntu/Debian-based systems:
    sudo apt update
    sudo apt install zenity

Tip

Use zenity to create pop-up notifications in Linux for eye strain reminders or other tasks. You can easily customize the script for other notifications too.

2. Create the notify.sh Script

  1. Create the script file:

    sudo nano /usr/local/bin/notify.sh
  2. Paste the contents of the script (from above) into the file.

  3. Make the script executable:

    sudo chmod +x /usr/local/bin/notify.sh

3. Create a systemd User Service

Now, let's create a systemd user service to run this script at startup.

  1. Create a directory for user systemd services (if it doesn't exist already):

    mkdir -p ~/.config/systemd/user/
  2. Create a new systemd service file:

    nano ~/.config/systemd/user/eye-reminder.service
  3. Add the following content to the service file:

[Unit]
Description=Eye care reminder
After=graphical.target

[Service]
Type=simple
ExecStart=/bin/bash /usr/local/bin/notify.sh
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/YOUR_USER/.Xauthority
Restart=always

[Install]
WantedBy=default.target

Replace YOUR_USER with your actual username.

Warning

The script uses Zenity, which requires a GUI to display pop-up notifications. Ensure your system starts a graphical session at boot, and the service starts after the GUI is up (After=graphical.target).

4. Enable the Service on Startup

Enable and start the service in user mode so that it runs automatically on system boot.

# Enable the service
systemctl --user enable eye-reminder.service

# Start the service immediately
systemctl --user start eye-reminder.service

Important

Testing the Service
After starting the service, test if the reminder pop-up appears. If it does not, verify that DISPLAY=:0 is correct for your session. You can check it with:

echo $DISPLAY

5. Check Status

To verify if the service is running:

systemctl --user status eye-reminder.service

Caution

If the pop-up notifications are not working, ensure that your user session is correctly set up and that the service is configured to run under your user's environment.


Additional Notes

Tip

You can adjust the sleep duration in the script (sleep 1140) to modify how often the reminder appears. By default, it shows every 19 minutes.

Warning

Frequent pop-ups may interfere with your workflow. Adjust the reminder intervals as needed to suit your work habits.

Note

If you want to stop or disable the reminder in the future, use the following commands:

  • To stop: systemctl --user stop eye-reminder.service
  • To disable: systemctl --user disable eye-reminder.service --now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment