A Bash script that monitors New Zealand earthquakes using the GeoNet API and sends notifications via ntfy.
- Monitors earthquakes in New Zealand based on MMI (Modified Mercalli Intensity) level.
- Sends notifications via ntfy with customizable priority.
- Prevents duplicate notifications.
- Sorts earthquakes chronologically (oldest to newest).
- Includes error checking and dependency validation.
The script requires the following dependencies:
- curl
- jq
- Download the script:
sudo curl -o '/usr/local/bin/geonet-earthquake-monitor' 'https://raw.githubusercontent.com/clove3am/geonet-earthquake-monitor/main/geonet-earthquake-monitor.sh'
- Make it executable:
chmod +x '/usr/local/bin/geonet-earthquake-monitor'
- Set up the required environment variables:
# Add these to your ~/.bashrc or equivalent
export NTFY_TOKEN_DEVICES="your-ntfy-token"
export NTFY_GEONET_URL="https://ntfy.sh/your-topic"
geonet-earthquake-monitor -m MMI -p PRIORITY
-m, --mmi
: Minimum MMI intensity (0-8)-p, --priority
: Notification priority (1-5)
-h, --help
: Show help message-v, --version
: Show version information
Monitor earthquakes with MMI ≥ 4 with high priority notifications:
geonet-earthquake-monitor -m 4 -p 4
Monitor all earthquakes with low priority notifications:
geonet-earthquake-monitor -m 0 -p 1
To run the script every 5 minutes using cron:
- Edit your crontab:
crontab -e
- Add the following line:
HOME=/home/your-use-name
NTFY_TOKEN_DEVICES=your-token-here
NTFY_GEONET_URL=https://ntfy.sh/your-topic
*/5 * * * * /usr/local/bin/geonet-earthquake-monitor -m 4 -p 4
- Create a service file:
cat <<'EOF' | sudo tee '/etc/systemd/system/geonet-earthquake-monitor.service' >/dev/null
[Unit]
Description=GeoNet Earthquake Monitor
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/bin/geonet-earthquake-monitor -m 4 -p 4
__HOME__
Environment=NTFY_TOKEN_DEVICES=your-token-here
Environment=NTFY_GEONET_URL=https://ntfy.sh/your-topic
Restart=always
RestartSec=60
[Install]
WantedBy=multi-user.target
EOF
sudo sed -i "s|__HOME__|Environment=HOME=$(echo ${HOME})|" '/etc/systemd/system/geonet-earthquake-monitor.service'
- Create a timer file:
cat <<'EOF' | sudo tee '/etc/systemd/system/geonet-earthquake-monitor.timer' >/dev/null
[Unit]
Description=Run GeoNet Earthquake Monitor every 5 minutes
[Timer]
OnBootSec=1min
OnUnitActiveSec=5min
Unit=geonet-earthquake-monitor.service
[Install]
WantedBy=timers.target
EOF
- Enable and start the timer:
sudo systemctl enable --now geonet-earthquake-monitor.timer
- Check status:
sudo systemctl status geonet-earthquake-monitor.timer
sudo systemctl list-timers --all
Variable | Description |
---|---|
NTFY_TOKEN_DEVICES |
Authentication token for ntfy |
NTFY_GEONET_URL |
The ntfy URL to publish to (e.g., https://ntfy.sh/earthquakes ) |
Notifications include:
- Title: Magnitude and location (e.g., "4.9M earthquake 15 km east of Picton")
- Body: Depth and time (e.g., "Depth: 35km at Mon Nov 4 2024 15:58")
- Click action: Links to the GeoNet earthquake page
The script maintains a cache of sent notifications at:
~/.local/state/geonet-earthquake-monitor/notified.cache
The script uses the GeoNet API endpoint:
https://api.geonet.org.nz/quake?MMI=[level]
API Features:
- Returns earthquakes from the last 365 days
- Maximum of 100 earthquakes per response
- MMI values range from -1 to 8
Contributions to improve the script are welcome! Please feel free to submit issues or pull requests on the project's repository.
This script is released under the MIT License. See the LICENSE file for details.