惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Security Latest
Security Latest
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
W
WeLiveSecurity
I
Intezer
Attack and Defense Labs
Attack and Defense Labs
Google Online Security Blog
Google Online Security Blog
S
Schneier on Security
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
Know Your Adversary
Know Your Adversary
N
News and Events Feed by Topic
K
Kaspersky official blog
NISL@THU
NISL@THU
Recent Commits to openclaw:main
Recent Commits to openclaw:main
M
Microsoft Research Blog - Microsoft Research
S
Secure Thoughts
罗磊的独立博客
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
Project Zero
Project Zero
Latest news
Latest news
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
L
LangChain Blog
PCI Perspectives
PCI Perspectives
博客园 - Franky
P
Palo Alto Networks Blog
A
Arctic Wolf
Hugging Face - Blog
Hugging Face - Blog
量子位
L
LINUX DO - 热门话题
人人都是产品经理
人人都是产品经理
T
Tor Project blog
博客园 - 叶小钗
C
CERT Recently Published Vulnerability Notes
李成银的技术随笔
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 三生石上(FineUI控件)
Scott Helme
Scott Helme
雷峰网
雷峰网

DEV Community

Class and Pseudo Class Git & GitLab Basics 고객은 우리를 사기꾼으로 봤다: 아무도 믿지 않는 신사업을 단 둘이서 검증한 3개월 Cache Everything: Advanced Caching Strategies in Vue 3 & Nuxt 4 Deploy a Node.js App to STACKIT Kubernetes Engine With Managed Redis & PostgreSQL Slopsquatting & Remote Prompts: Why I Built a 38,000 Ticker Engine with Zero NPM Dependencies 05/20: TCP/IP vs OSI Model: The Ultimate Comparison My New Adventures in IT # Mitigating Market Inefficiency in eSports: A Stochastic Approach to EA Sports FC25 Modeling Don't let a billion RAG docs drown your 25-result pipeline Experienced devs are slower with AI tools. Nobody wants to admit it. I built an MCP-native OSINT framework that lets AI agents investigate from your terminal AWS Nitro Enclaves vs Intel TDX: Why Attestation Root Matters for Regulated Workloads Vibe Coding: Revolution or Risk in Software Development? - SmarterArticles S1E6 JSON Schema Explained: Validate Your API Data Before It Breaks Production Harness Tells Your Agent What to Do. GUI Agents Let It Actually Do It. Is AI actually replacing developers? Customizing Docker Images: Write Your First Dockerfile (2026) €40 n8n vs 28% weekly Anthropic quota. Which /goal layer should you actually run? Reviving glyph-v8: From a Forgotten Prototype to STRIDE - a Field-Aware Integer Coder 04/20: Data Encapsulation: How a Message Becomes Bits on the Wire Hướng Dẫn Thiết Lập Reasoning Proxy DeepSeek V4-Pro với Cursor (2026) Sofi Log #012: Agentic GDP — Solana Pay.sh & x402 Protocol Spec Input Types, Attributes, Self-Closing Tags, Hover Effect Absolute vs Relative Paths File Types (Regular, Directory, Link, Device, Socket, Pipe) From Arduino IDE to AVR GCC | AVR Bare Metal #1 Using Bitcoin as collateral without wrapping it: the design of a BTC collateral vault Unreal Engine 5 Skill System Architecture using GAS and GameplayTags 5 Things I Wish I Knew Before Building with Hermes Agent Thoughts on Codingame 2026 Spring challenge OUT WITH THE OLD IN WITH THE NEW Why are simple 1099 tax calculators online so horribly bloated? So I built my own "Why You're Not Getting Callbacks (It's Not Your Skills)" # How I Built a Retail Demand Forecasting App with Python and Streamlit Why We Deliberately Crush Lithium Batteries (UN38.3 Crush Testing Explained) Command History & Completion The Three-Body Problem: AI Code, Supply Chain Attacks, and the Talent Exodus 로컬 LLM 셋업 가이드 (v27) Building Better .NET Worker Services with Cursor Rules Generate Professional PDF Invoices via REST API — JSON In, PDF Out Redis: Big Keys Destroem o Desempenho Compartilhado Agentic AI for Cybersecurity: Autonomous Threat Detection and Response How to Automate Android Without Appium Cron vs systemd daemon: which one for Node.js? Designing XSLT transforms with parameters and multiple inputs I Downloaded Gemma4:e2b On My Macbook in 2 steps Building an Autonomous SRE Agent: From Raw Telemetry to Safe, AI-Driven Remediation The EU AI Act in 2026: Reading the Law After the Omnibus I had zero coding knowledge. Here is "RetroTube", a 2010 YouTube sandbox prototype I built using AI! How to Validate Environment Variables in TypeScript (and Why You Should) I Built a CLI Tool That Writes Better Git Commits Than I Do Transfer Fees, Metadata, and Soulbound Tokens: My First Real Token Experiments on Solana Stop Using Fetch() in React: A Better Way To Call Your Backend Creando un Tetris con JavaScript VI: Complicando el juego. DeepSeek's API Price Cut Changed My Claude Code and ChatGPT Math [Boost] Perl 🐪 Weekly #774 - Perl is too HOT How to Track AI Usage Without Losing Revenue (Complete Guide) 77 Rules Later: What Graduating Our First Stack Actually Looked Like RAG 시스템 실전 구축 (v26) When Premature Scaling Leads to Operator Burnout Multi-Repo Microservice Changes Are a Coordination Problem. I Solved It With AI Agent Teams. The Next Frontier: How Multi-Agent Systems are Redefining Productivity The Kimwolf Bust Just Outed Android Webcams as Botnet Fodder — Here's the Question Every Repurposed-Phone Camera Setup Has to Answer I'm an autonomous AI agent. I shipped 18 fixes to myself in one session. Building a Secure Future with Zero Trust Security Architecture Asynchronous Functions in Dart How I migrated magic-link login from Resend to AWS SES + Lambda five days before launch Edge Computing He creado una empresa ficticia IT/OT para poder encontrar sus vulnerabilidades y reforzar su seguridad en sus activos críticos Why I Built @editora/react I built a tiny UGC script generator because hooks are the hardest part The Phone Is Becoming the New Terminal Why Most AI Music Tools Feel Wrong to Developers Goroutines vs. Promises: Why Go and JavaScript Look at Concurrency Completely Differently How I Use Antigravity 2.0 to Navigate Open-Source Codebases and Make Better Technical Decisions Understanding Basic HTML & CSS Concepts for Beginners Go Error Handling: Annoying or Awesome? Your To-Do List Doesn't Know You — So I Gave Mine Three Brains Shell Basics (Bash, Zsh, Sh) Free MongoDB GUI Tool for Developers, Students, and Teams Designing High-Performance Blockchain Indexers Choosing Models for an Agentic Chat App on Amazon Bedrock How Smart Growth Teams Automate Their Marketing Stack in 2026 (Without Hiring More People) What I Learned About Memory-Augmented AI Agents Seven Docker Tips Every Engineer Should Know (from Docker Captains) Welcome to the Fast-Food Era of Testing: Over-Weight by Tests How to use Claude in vscode? Prompt Engineering for Automated Evaluation: Making LLMs the Judge in AI Builder Solutions Full Stack Projects Are Not Enough Anymore Virtualization & Cloud Basics Orakle: Turning Raw Blockchain Data into Intelligence with Gemma 4 Building an Autoposting Pipeline with Hermes Agent: Why Waterfall Beats Parallel, and the Edge Cases Nobody Talks About OpenShift Virtualization Migration Advisor — Local-First, Powered by Gemma 4 26B MoE WebMCP is coming — so I’m building webmcp.js I Disappeared for 4 Months After Launch - Here's What Brought Me Back Jira Is Turing-Complete (And You've Been Coding in It) NyayAI: Building an AI Legal Assistant for 1.4 Billion People — A Technical Deep Dive E-commerce Order Automation: Stripe + Invoice + Shipping Workflow
Cron Not Working on Mac? How to Fix the macOS Sleep Trap with launchd
Arjun Adhika · 2026-05-25 · via DEV Community

If you've ever built an automated script like a Python crypto price scraper, a daily backup script, or an RSS archiver, you probably felt like a genius right up until the moment it failed to run.

You set up your crontab perfectly. You told it to run every day at 6:30 PM. You tested the script in your terminal, and it worked flawlessly. But then you check your logs a few days later, and... crickets. Nothing happened.

What gives?

If you are running your automation on a personal Mac, you've just run into the classic Sleep Mode Trap.


Why Your Mac is Ignoring Your Cron Jobs

cron is a legendary piece of software. It has been the backbone of Unix automation for decades. But cron was designed for servers that live in cold, dark rooms and literally never go to sleep.

Your Mac is different. When you close the lid or walk away for an hour, your Mac goes to sleep to save battery.

Here is how cron handles sleep: It doesn't.

If you have a job scheduled for 6:30 PM and your laptop is asleep at 6:30 PM, cron simply shrugs and skips it. It won't try again until tomorrow at 6:30 PM. If your laptop is closed tomorrow at 6:30 PM too? Skipped again. Your "automated" pipeline is now completely broken unless you babysit it and keep your screen awake.


Apple knew this was a problem, which is why they built launchd.

launchd is the modern, macOS-native replacement for cron and init. It manages everything running in the background on your Mac. Unlike cron, launchd is generally much better at handling sleep/wake scenarios — it can run missed scheduled jobs shortly after wake, depending on your exact power state and configuration.

Cron vs. launchd at a Glance

Feature cron launchd
Handles sleep well
Native macOS support
Simple syntax
Good for servers
Good for personal laptops

LaunchAgents vs. LaunchDaemons: What's the Difference?

Before writing your configuration, it's crucial to know where to put it. launchd separates jobs into two categories:

  1. LaunchDaemons (/Library/LaunchDaemons/) — Run as the root system user as soon as the Mac boots, even before anyone logs in. Use this for system-level services like database servers or network listeners.
  2. LaunchAgents (~/Library/LaunchAgents/) — Run as your specific user account, only after you log in. Since personal scripts need access to your files, home directory, and user permissions, LaunchAgents are almost always what you want.

How launchd Actually Works Under the Hood

launchd is the absolute core of macOS. When you turn on your Mac, launchd is the very first process that starts — it has Process ID 1. Every other application on your Mac is technically a "child" of launchd.

When your Mac sleeps, the hardware clock keeps ticking. When you open your laptop, the Kernel updates the system time and launchd checks its internal schedule. If it missed a StartCalendarInterval job during the sleep window, it triggers the job shortly after wake to catch up.


Step-by-Step: Migrating from Cron to launchd

Instead of editing a crontab, you create a Property List (.plist) XML configuration file and drop it in ~/Library/LaunchAgents/.

Here is a complete, production-ready template for a Python script that runs Monday through Friday at 6:30 PM.

File: ~/Library/LaunchAgents/com.yourname.dailyscript.plist

<?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>

    <!-- 1. Unique identifier for this job -->
    <key>Label</key>
    <string>com.yourname.dailyscript</string>

    <!-- 2. Command to run — always use absolute paths for the interpreter AND the script -->
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/python3</string>
        <string>/Users/yourname/projects/my_script.py</string>
    </array>

    <!-- 3. Working directory — prevents "file not found" errors inside your script -->
    <key>WorkingDirectory</key>
    <string>/Users/yourname/projects</string>

    <!-- 4. Environment variables — launchd starts with a minimal PATH, so declare it explicitly -->
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    </dict>

    <!-- 5. Schedule: Monday (1) through Friday (5) at 18:30 -->
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Weekday</key><integer>1</integer>
            <key>Hour</key><integer>18</integer>
            <key>Minute</key><integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key><integer>2</integer>
            <key>Hour</key><integer>18</integer>
            <key>Minute</key><integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key><integer>3</integer>
            <key>Hour</key><integer>18</integer>
            <key>Minute</key><integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key><integer>4</integer>
            <key>Hour</key><integer>18</integer>
            <key>Minute</key><integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key><integer>5</integer>
            <key>Hour</key><integer>18</integer>
            <key>Minute</key><integer>30</integer>
        </dict>
    </array>

    <!-- 6. Persistent log files — use ~/Library/Logs, NOT /tmp (which gets wiped by macOS) -->
    <key>StandardOutPath</key>
    <string>/Users/yourname/Library/Logs/dailyscript.out</string>
    <key>StandardErrorPath</key>
    <string>/Users/yourname/Library/Logs/dailyscript.err</string>

</dict>
</plist>

Enter fullscreen mode Exit fullscreen mode

Understanding the Key Fields

Key What it does
Label Unique name used to identify and manage the job
ProgramArguments Exact command and arguments — equivalent to typing in your terminal
WorkingDirectory Sets the pwd inside your script; prevents relative path failures
EnvironmentVariables Injects env vars — critical because launchd starts with a barebones $PATH
StartCalendarInterval Defines the schedule; 0 and 7 are both Sunday
StandardOutPath Where print() and stdout output goes
StandardErrorPath Where errors and tracebacks go

Why ~/Library/Logs/ instead of /tmp/?
macOS periodically cleans /tmp/ — sometimes as frequently as on every reboot. If you put your logs there, you could lose them before you ever read them. ~/Library/Logs/ is the macOS convention for persistent, user-level logs.


Activating the Job (The Modern Way)

Historically, most tutorials teach launchctl load, but Apple considers this legacy. The modern, recommended way to register a LaunchAgent for your user session is:

# Register the job
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.yourname.dailyscript.plist

# To test it immediately without waiting for the scheduled time
launchctl start com.yourname.dailyscript

# If you edit the plist, you must unload and reload it
launchctl bootout gui/$(id -u) ~/Library/LaunchAgents/com.yourname.dailyscript.plist
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.yourname.dailyscript.plist

Enter fullscreen mode Exit fullscreen mode


Common launchd Gotchas

If your script isn't running, it is almost certainly one of these:

  1. Barebones $PATHlaunchd starts with a minimal environment. If your script calls python, node, or any Homebrew tool by name without a full path, it will silently fail. Always declare your PATH explicitly in EnvironmentVariables, and on Apple Silicon Macs include /opt/homebrew/bin.

  2. Absolute paths everywherelaunchd does not expand ~. Use /Users/yourname/ literally everywhere — in ProgramArguments, WorkingDirectory, and log paths.

  3. Scripts fail silently — There is no terminal window to show you errors. Always check your log files after the first run:

    cat ~/Library/Logs/dailyscript.err
    
  4. Verify the job is registered:

    launchctl list | grep com.yourname.dailyscript
    

    The three columns are: PID (blank if not running), last exit code (0 = success), and label.

  5. Stream live system logs for deeper debugging:

    log stream --predicate 'subsystem == "com.apple.launchd"'
    

A Quick Note on KeepAlive

As you explore launchd further, you will encounter <key>KeepAlive</key>. Setting it to <true/> tells launchd to treat your script as a permanent background daemon — if it crashes or exits, launchd will immediately restart it. This is exactly what you want for a persistent web server, but completely wrong for a daily scraper that is supposed to run once and exit.


What About Windows?

Windows has its own built-in equivalent: Task Scheduler.

To enable the same "catch-up on wake" behavior:

  1. Open Task Scheduler and create a new task.
  2. Set your trigger (daily, weekly, etc.).
  3. Go to the Settings tab.
  4. Check "Run task as soon as possible after a scheduled start is missed".

That single checkbox gives you the same sleep-safe behavior as launchd's StartCalendarInterval. Windows also supports environment variables and working directory settings under the task's Actions configuration.


Final Thoughts

cron is a classic for a reason — simple, powerful, and universal. But it was built for servers that never sleep. If you are building personal automation on a laptop, you owe it to yourself to use the scheduler your OS was actually designed around.

Your data (and your sanity) will thank you.