← All Lessons
Lesson 07

The Night Shift

Set a task before bed. Wake up to a branch full of completed work. This is headless Claude Code.

You've been using Claude Code interactively. Typing prompts, reviewing outputs, approving changes. But Claude Code can also run without you. Completely unattended. You describe the task, walk away, and review the results in the morning.


What you're building today

A script that runs Claude Code headlessly on any project. You give it a task and a folder, it creates a git branch, does the work, commits, and logs everything. You review the branch when you're ready.


Step 1: How headless mode works

Claude Code has a flag called -p (pipe mode). Instead of opening an interactive session, it takes a prompt as input and runs it straight through. No approval prompts, no back-and-forth. Just execution.

claude -p "Your task description here" --dangerously-skip-permissions

The --dangerously-skip-permissions flag lets Claude approve its own tool calls. Without it, Claude would pause and wait for you to approve every file edit, which defeats the purpose of unattended execution.


Step 2: The overnight agent script

This is a real script. Copy it, save it, use it tonight.

#!/bin/bash
# Overnight Agent — Run Claude Code headlessly on a task
#
# Usage:
#   ./overnight-agent.sh "Fix the slow dashboard loading" ~/projects/myapp
#   ./overnight-agent.sh "Add dark mode support" ~/projects/myapp 30
#
# Args:
#   $1 — Task description (required)
#   $2 — Project directory (required)
#   $3 — Max turns (optional, default: 20)

set -euo pipefail

TASK="${1:?Usage: overnight-agent.sh \"task description\" /path/to/project [max-turns]}"
PROJECT_DIR="${2:?Usage: overnight-agent.sh \"task description\" /path/to/project [max-turns]}"
MAX_TURNS="${3:-20}"

TIMESTAMP=$(date +%Y-%m-%d_%H%M)
LOG_DIR="$HOME/strategy/logs/overnight"
LOG_FILE="$LOG_DIR/$TIMESTAMP.log"

mkdir -p "$LOG_DIR"

echo "=== Overnight Agent ===" | tee "$LOG_FILE"
echo "Task:    $TASK" | tee -a "$LOG_FILE"
echo "Project: $PROJECT_DIR" | tee -a "$LOG_FILE"
echo "Turns:   $MAX_TURNS" | tee -a "$LOG_FILE"
echo "Started: $(date)" | tee -a "$LOG_FILE"
echo "Log:     $LOG_FILE" | tee -a "$LOG_FILE"
echo "===" | tee -a "$LOG_FILE"

# Unset to allow nested invocation
unset CLAUDE_CODE 2>/dev/null || true
unset CLAUDECODE 2>/dev/null || true

cd "$PROJECT_DIR"

# Run Claude Code headlessly
claude -p "$TASK

After completing the task:
1. Create a new git branch named overnight/$TIMESTAMP
2. Commit all changes with a clear message
3. Do NOT push or create a PR — just commit locally
4. Print a summary of what you changed and why" \
  --dangerously-skip-permissions \
  --max-turns "$MAX_TURNS" \
  2>&1 | tee -a "$LOG_FILE"

echo "" | tee -a "$LOG_FILE"
echo "=== Finished: $(date) ===" | tee -a "$LOG_FILE"
echo "Log saved to: $LOG_FILE"

Step 3: Install it

# Save the script
mkdir -p ~/strategy/scripts/night-shift
# (paste the script above into ~/strategy/scripts/night-shift/overnight-agent.sh)

# Make it executable
chmod +x ~/strategy/scripts/night-shift/overnight-agent.sh

# Create the log directory
mkdir -p ~/strategy/logs/overnight

Step 4: Run it

# Before bed — give it a task and a project folder
~/strategy/scripts/night-shift/overnight-agent.sh \
  "Clean up all console.log statements and unused imports" \
  ~/strategy/projects/my-project

Then close your laptop lid. Or don't. Either way, Claude is working.


Step 5: Review in the morning

# Check what happened
cd ~/strategy/projects/my-project
git log --oneline -5

# See the overnight branch
git branch | grep overnight

# Review the changes
git diff main..overnight/2026-04-06_2300

The changes are on a branch. Nothing touches main. You review, cherry-pick what you like, throw away what you don't.

Start small. Don't ask it to "rebuild the architecture." Give it bounded, verifiable tasks: clean up console.log statements and unused imports, add TypeScript types to untyped functions, write tests for the 3 most critical functions, update outdated comments and docstrings, resolve TODO comments one by one.


The safety model

The script never pushes. It never touches main. It commits to an overnight/ branch that you review manually. The --max-turns flag prevents runaway sessions. The log file captures everything Claude did so you can audit it. This is designed for trust, not blind faith.

What you now know

Homework

Tonight, run the overnight agent on one of your projects. Pick the simplest task you can think of. "Add a comment to every function that doesn't have one." Wake up, check the branch, see how it did. That's day one. By day seven you'll be handing it real work.