Skip to content

Instantly share code, notes, and snippets.

@dalexsoto
Last active August 14, 2025 18:05
Show Gist options
  • Select an option

  • Save dalexsoto/544a017bbcb59d5141bf5d2c04c05c73 to your computer and use it in GitHub Desktop.

Select an option

Save dalexsoto/544a017bbcb59d5141bf5d2c04c05c73 to your computer and use it in GitHub Desktop.
Enable screen sharing and keep it enabled by checking every 30 minutes, Thanks to AI for doing all of this. Remove `xattr -r -d com.apple.quarantine .` then `chmod +x install-ensure-screensharing.sh` and run
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.alexsoto.ensure-screensharing</string>
<!-- Use absolute paths only. If your script needs a shell, call it explicitly. -->
<key>ProgramArguments</key>
<array>
<string>/bin/zsh</string>
<string>/usr/local/sbin/ensure-screensharing.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>1800</integer>
<!-- Logs (keep it simple). launchd will create them. -->
<key>StandardOutPath</key>
<string>/var/log/ensure-screensharing.log</string>
<key>StandardErrorPath</key>
<string>/var/log/ensure-screensharing.log</string>
</dict>
</plist>
#!/bin/zsh
set -euo pipefail
LOG="/var/log/ensure-screensharing.log"
exec >>"$LOG" 2>&1
echo "[$(date)] watchdog tick"
# 1) Ensure the Screen Sharing launch daemon exists + is enabled
if ! /bin/launchctl print system/com.apple.screensharing >/dev/null 2>&1; then
/bin/launchctl enable system/com.apple.screensharing || true
/bin/launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist || true
fi
# 2) If disabled, enable it
if /bin/launchctl print system/com.apple.screensharing 2>&1 | /usr/bin/grep -q "state = disabled"; then
/bin/launchctl enable system/com.apple.screensharing
fi
# 3) Make sure it’s actually ready to accept connections (port 5900)
if ! /usr/sbin/lsof -nP -iTCP:5900 -sTCP:LISTEN >/dev/null 2>&1; then
/bin/launchctl kickstart -k system/com.apple.screensharing || true
fi
#!/bin/bash
# filepath: install-ensure-screensharing.sh
set -euo pipefail
LABEL="com.alexsoto.ensure-screensharing"
SCRIPT_NAME="ensure-screensharing.sh"
PLIST_NAME="${LABEL}.plist"
DEST_DIR="/usr/local/sbin"
DEST_SCRIPT="${DEST_DIR}/${SCRIPT_NAME}"
DEST_PLIST="/Library/LaunchDaemons/${PLIST_NAME}"
log() { printf '[*] %s\n' "$*" >&2; }
err() { printf '[!] %s\n' "$*" >&2; }
die() { err "$*"; exit 1; }
# 1. Root (sudo) check
if [[ ${EUID} -ne 0 ]]; then
err "This installer must run as root. Re-running with sudo..."
exec sudo "$0" "$@"
fi
# Resolve directory containing this installer to locate source files
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SRC_SCRIPT="${SCRIPT_DIR}/${SCRIPT_NAME}"
SRC_PLIST="${SCRIPT_DIR}/${PLIST_NAME}"
[[ -f "${SRC_SCRIPT}" ]] || die "Missing ${SRC_SCRIPT}"
[[ -f "${SRC_PLIST}" ]] || die "Missing ${SRC_PLIST}"
# 2. Ensure /usr/local/sbin exists with correct perms
log "Ensuring ${DEST_DIR} exists"
install -d -m 755 "${DEST_DIR}"
# 3 & 4. Move files into place
log "Installing script to ${DEST_SCRIPT}"
mv -f "${SRC_SCRIPT}" "${DEST_SCRIPT}"
log "Installing plist to ${DEST_PLIST}"
mv -f "${SRC_PLIST}" "${DEST_PLIST}"
# 5. Fix permissions
log "Setting ownership and permissions"
chown root:wheel "${DEST_PLIST}"
chmod 644 "${DEST_PLIST}"
chown root:wheel "${DEST_SCRIPT}"
chmod 755 "${DEST_SCRIPT}"
# 6. launchctl operations
log "Unloading (bootout) existing job (ignore errors)"
launchctl bootout system/${LABEL} 2>/dev/null || true
launchctl bootout system "${DEST_PLIST}" 2>/dev/null || true
log "Bootstrapping job"
launchctl bootstrap system "${DEST_PLIST}"
log "Enabling job"
launchctl enable system/${LABEL}
log "Kickstarting job"
launchctl kickstart -k system/${LABEL}
log "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment