Script to Check Internet Connection and Create Notification on Apple Mac
- Categories:
- notes
I’ve decided to create a script for my Mac to check internet connection and create notifications. After some research, surprisingly it’s very easy and I only need a few lines of shell script.
To trigger notification on Apple Mac only needs one line of Applescript. AppleScript can be run from the shell using /usr/bin/osascript
.
I use the ping
command to check internet connection. If the exit status is equal to zero, it means my Mac is connected to the internet.
Below is the completed script, you can get it on Github.
#!/bin/bash
while true
do
# -c <number>: Stop after sending <number> packages
# -q: quiet output
ping -c 3 -q google.com > /dev/null
exit_status=$?
if [ $exit_status -eq 0 ]; then
echo "connected to the internet"
osascript -e 'display notification "check_mac_internet.sh" with title "Connection Status" subtitle "Connected to the Internet! Yay!" sound name "Hero"'
fi
sleep 10
done
References:
Recent Posts
AWS Secrets Manager
Explanation about AWS Secrets Manager with example code.
Envelope Encryption
Envelope encryption is the practice of encrypting plaintext data with a data key, and then encrypting the data key under another key.
Tutorial - "su username vs su - username" - A Security Perspective
The main difference between `su username` and `su - username` lies in the environment variables that are loaded when switching to the specified user.
Subdomain Hijacking
My dormant subdomain was recently hijacked, redirecting it to a online gamble registration page.
C# DbContext ServiceLifeTime
my note about C Sharp ServiceLifeTime