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. π
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:
- Which days should Discord stay open? (e.g. MonβFri)
- 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.
| File | Goes to |
|---|---|
discord-workhours-watch.sh |
~/.local/bin/discord-workhours-watch.sh |
com.user.DiscordWorkHours.plist |
~/Library/LaunchAgents/com.user.DiscordWorkHours.plist |
-
Create the folders:
mkdir -p ~/.local/bin ~/Library/LaunchAgents
-
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 -
Add the agent at
~/Library/LaunchAgents/com.user.DiscordWorkHours.plist.β οΈ In that file replaceYOUR_USERNAMEwith your macOS short username (runecho $USERto see it). launchd needs a full path β~won't work. -
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.
Check status:
launchctl print gui/$(id -u)/com.user.DiscordWorkHours | grep stateTurn it off (unload the agent):
launchctl bootout gui/$(id -u)/com.user.DiscordWorkHoursTurn it back on:
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.user.DiscordWorkHours.plist- 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
KeepAlivein the plist instead of a watcher script.