Skip to content

Instantly share code, notes, and snippets.

@Lysak
Last active July 18, 2026 15:43
Show Gist options
  • Select an option

  • Save Lysak/ff621ec3c2949a9158c116e03a8ac718 to your computer and use it in GitHub Desktop.

Select an option

Save Lysak/ff621ec3c2949a9158c116e03a8ac718 to your computer and use it in GitHub Desktop.
Keep Discord running during work hours only on macOS (launchd; schedule set at the top of the script)

Keep Discord running during work hours only (macOS Β· launchd)

A tiny macOS setup that keeps Discord always open during your work hours and reopens it automatically if you close it or it crashes. Outside those hours (evenings, weekends) it does nothing, so you can quit Discord normally.

How it works: a launchd agent runs a small watcher script every 30 seconds. The script checks the current day/hour; if you're inside your window and Discord isn't running, it reopens it in the background (open -g, so it never steals focus).

The schedule (days + hours) is set once, right at the top of the script β€” no separate config file, nothing to load. Change it when you change jobs. πŸ™‚


πŸ€– Setting this up with an AI agent?

If you pasted this gist to an AI agent (Claude Code, etc.) and asked it to install everything for you β€” the agent should, before touching anything, ask you two questions and wait for your answer:

  1. Which days should Discord stay open? (e.g. Mon–Fri)
  2. Which hours? Start and end in 24h format (e.g. 09:00 β†’ 18:00)

Then it writes those into the top of the script, installs the files, replaces YOUR_USERNAME, loads the agent, and verifies Discord reopens after being quit. If your agent skips the questions and just assumes Mon–Fri 9–18, tell it to ask.


πŸ“¦ Files

File Goes to
discord-workhours-watch.sh ~/.local/bin/discord-workhours-watch.sh
com.user.DiscordWorkHours.plist ~/Library/LaunchAgents/com.user.DiscordWorkHours.plist

βš™οΈ Installation

  1. Create the folders:

    mkdir -p ~/.local/bin ~/Library/LaunchAgents
  2. Add the watcher script at ~/.local/bin/discord-workhours-watch.sh. Open it and edit the three lines at the top to set your schedule:

    WORK_DAYS="1 2 3 4 5"   # 1=Mon … 7=Sun
    START_HOUR=9            # from 09:00
    END_HOUR=18            # up to 18:00 (not inclusive)

    Then make it executable:

    chmod +x ~/.local/bin/discord-workhours-watch.sh
  3. Add the agent at ~/Library/LaunchAgents/com.user.DiscordWorkHours.plist. ⚠️ In that file replace YOUR_USERNAME with your macOS short username (run echo $USER to see it). launchd needs a full path β€” ~ won't work.

  4. Load the agent:

    launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.user.DiscordWorkHours.plist

Done. Discord will now stay open during your configured window.

Changed the schedule later? Just edit the top of the script β€” the next check (within 30 s) uses the new values. No reload needed.


πŸ”„ Management

Check status:

launchctl print gui/$(id -u)/com.user.DiscordWorkHours | grep state

Turn it off (unload the agent):

launchctl bootout gui/$(id -u)/com.user.DiscordWorkHours

Turn it back on:

launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.user.DiscordWorkHours.plist

❓ Notes

  • Discord is an Electron app with many helper processes. The script matches only the main process (Discord.app/Contents/MacOS/Discord), so a stray helper won't fool it into thinking Discord is still open.
  • Want Discord to also quit automatically at the end of the window? That's an optional add-on β€” ask.
  • Prefer a 24/7 "always on" app instead (no schedule)? Use KeepAlive in the plist instead of a watcher script.
<?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.user.DiscordWorkHours</string>
<key>LimitLoadToSessionType</key>
<string>Aqua</string>
<key>ProgramArguments</key>
<array>
<!-- IMPORTANT: replace YOUR_USERNAME with your macOS short username (echo $USER) -->
<string>/Users/YOUR_USERNAME/.local/bin/discord-workhours-watch.sh</string>
</array>
<!-- run the check every 30 seconds -->
<key>StartInterval</key>
<integer>30</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
#!/bin/bash
# Discord Work-Hours Watcher
# Keeps Discord running only during the days/hours set below. If Discord is
# closed or crashes inside that window, it is reopened in the background
# (without stealing focus). Outside the window it does nothing.
# ============================================================================
# EDIT THESE THREE LINES β€” this is the only configuration.
# ============================================================================
WORK_DAYS="1 2 3 4 5" # days: 1=Mon 2=Tue 3=Wed 4=Thu 5=Fri 6=Sat 7=Sun
START_HOUR=9 # start of window (24h). 9 = 09:00
END_HOUR=18 # end of window (24h, not inclusive). 18 = up to 18:00
# ============================================================================
day=$(date +%u) # 1=Mon ... 7=Sun
hour=$((10#$(date +%H))) # base-10 to avoid octal issues with 08/09
in_days=0
for d in $WORK_DAYS; do
[ "$d" = "$day" ] && in_days=1
done
if [ "$in_days" = "1" ] && [ "$hour" -ge "$START_HOUR" ] && [ "$hour" -lt "$END_HOUR" ]; then
# match ONLY the main Discord process, not the Electron helper processes
if ! pgrep -f "Discord.app/Contents/MacOS/Discord$" >/dev/null 2>&1; then
/usr/bin/open -g -a Discord
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment