claude-code - 💡(How to fix) Fix [BUG] SessionStart hooks should complete before scheduled_tasks.json is loaded by cron scheduler [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#52550Fetched 2026-04-24 06:04:10
View on GitHub
Comments
0
Participants
1
Timeline
5
Reactions
0
Author
Participants
Timeline (top)
labeled ×5

When a SessionStart hook creates .claude/scheduled_tasks.json, the cron scheduler does not pick up the tasks until the second session restart. This is a race condition — the scheduler reads the file before the hook has finished writing it.

Use case

Enterprise/managed deployments use SessionStart hooks in managed-settings.json to provision recurring tasks (e.g., AWS credential refresh) for all users. The current race condition means the first session in any new workspace is broken.

Environment

  • Claude Code CLI on Linux and Windows
  • Managed settings via /etc/claude-code/managed-settings.json

Error Message

Error Messages/Logs

Root Cause

  1. Start Claude Code
  2. The cron task does not fire
  3. Restart Claude Code — now the task fires (because the file was already created previously)
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What's Wrong?

Summary

When a SessionStart hook creates .claude/scheduled_tasks.json, the cron scheduler does not pick up the tasks until the second session restart. This is a race condition — the scheduler reads the file before the hook has finished writing it.

Use case

Enterprise/managed deployments use SessionStart hooks in managed-settings.json to provision recurring tasks (e.g., AWS credential refresh) for all users. The current race condition means the first session in any new workspace is broken.

Environment

  • Claude Code CLI on Linux and Windows
  • Managed settings via /etc/claude-code/managed-settings.json

What Should Happen?

Durable scheduled tasks written by SessionStart hooks should be picked up in the same session, not requiring a restart.

Error Messages/Logs

Steps to Reproduce

  1. Add a SessionStart hook in managed settings (/etc/claude-code/managed-settings.json) that writes .claude/scheduled_tasks.json:
"hooks": {
  "SessionStart": [
    {
      "matcher": "",
      "hooks": [
        {
          "type": "command",
          "command": "bash -c 'mkdir -p \"$PWD/.claude\" && echo \"{\\\"tasks\\\":[{\\\"id\\\":\\\"aws-refresh\\\",\\\"cron\\\":\\\"*/5 * * * *\\\",\\\"prompt\\\":\\\"aws sts get-caller-identity > /dev/null 2>&1\\\",\\\"recurring\\\":true,\\\"createdAt\\\":$(date +%s)000}]}\" > \"$PWD/.claude/scheduled_tasks.json\"'"
        }
      ]
    }
  ]
}

2. Start Claude Code
3. The cron task does not fire
4. Restart Claude Code — now the task fires (because the file was already created previously)

### Claude Model

Opus

### Is this a regression?

No, this never worked

### Last Working Version

_No response_

### Claude Code Version

2.1.112

### Platform

AWS Bedrock

### Operating System

Other Linux

### Terminal/Shell

Xterm

### Additional Information

_No response_

extent analysis

TL;DR

The cron scheduler should be modified to wait for the SessionStart hook to finish writing the .claude/scheduled_tasks.json file before reading it.

Guidance

  • Verify that the SessionStart hook has completed execution before the cron scheduler attempts to read the .claude/scheduled_tasks.json file.
  • Consider introducing a delay or a synchronization mechanism to ensure the hook has finished writing the file.
  • Review the managed settings configuration to ensure the SessionStart hook is correctly defined and executed.
  • Test the fix by starting Claude Code, verifying the cron task does not fire, and then restarting Claude Code to confirm the task fires as expected.

Example

No code snippet is provided as the issue is related to the synchronization of the SessionStart hook and the cron scheduler.

Notes

The provided information suggests a race condition between the SessionStart hook and the cron scheduler. The fix should focus on ensuring the hook has completed execution before the scheduler attempts to read the .claude/scheduled_tasks.json file.

Recommendation

Apply a workaround to introduce a delay or synchronization mechanism to ensure the SessionStart hook has finished writing the .claude/scheduled_tasks.json file before the cron scheduler reads it. This will help mitigate the race condition and allow the cron task to fire as expected.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING