Skip to content

Instantly share code, notes, and snippets.

@smartwatermelon
Last active July 21, 2022 05:31
Show Gist options
  • Save smartwatermelon/b73507370c564ed03d0bfa2eb7b74d2a to your computer and use it in GitHub Desktop.
Save smartwatermelon/b73507370c564ed03d0bfa2eb7b74d2a to your computer and use it in GitHub Desktop.
start-synergy: Wait for connection to counterpart, then start Synergy.
#!/usr/local/bin/bash
set -eux -o pipefail
## this runs on both the client and server Mac
## configure CLIENT and SERVER appropriately
## brew install terminal-notifier
## use with e.g. ControlPlane (launch this script when Thunderbolt network connection becomes active)
CLIENT=my-client-mac.local
SERVER=the-server-mac.local
notif () {
[ -z "$1" ] && return 0
MSG="$1"
echo "${MSG}" | /usr/local/bin/terminal-notifier -title "${THIS}"
}
toLower () { tr '[:upper:]' '[:lower:]' <<< "$1"; }
if [ $( toLower "${HOSTNAME}" ) == $( toLower "${CLIENT}" ) ]; then TARGET="${SERVER}"; else TARGET="${CLIENT}"; fi
LOOP=0
TRIES=3
THIS="$( basename "$0" )" || THIS="start-synergy"
# wait one minute between tries, up to $TRIES tries
notif "Waiting for connection"
until ping -o "${TARGET}" || [ $LOOP -gt $TRIES ]; do sleep 60; (( LOOP++ )); done
( pkill -i synergy 2>/dev/null || : )
if open -nj /Applications/Synergy.app/ ; then
notif "Started Synergy"
else
notif "Failed to start Synergy"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment