# Mac exercise for mission 17.2: Morning note as a scheduled service

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/mac-17-2

About 20 minutes at your computer · Type this address: **llm.yoga/en/mac-17-2**

In the mission you played through the routine’s path. Here it really runs: `morgenzettel.py` (“morning note”) from chapter 17 sums up a calendar file in three bullet points with a local model, but only if the file is at most 24 hours old. Otherwise it writes what’s missing and ends with exit code 1. The calendar here is a plain text file; the appointments are an example. The script, its messages and the appointments are in German, as in the measurement.

**Before you start**

You need **Ollama** and Python 3 in the terminal. Ollama comes as an app: download it from ollama.com/download and drag it into your Applications folder. On first launch, the app asks permission, if needed, to set up the `ollama` terminal command. It requires macOS 14 or newer. More on this in [chapter 10](https://llm.yoga/en/10-lokale-modelle-4.md). Check:

```
ollama --version
```

Python 3 may need to be installed. On the first `python3` call, macOS may offer to install developer tools; alternatively, Python is available from python.org. The script only uses the standard library.

### 1. Load a small model

qwen3:1.7b is 1.4 GB and needs about 1.5 GB once loaded. That works even with 8 GB.

```
ollama pull qwen3:1.7b
```

### 2. Folder and today’s calendar

Create the folder `~/notizen` (“notes”) and in it a file heute.txt (“today”) with the day’s appointments (a meeting on an offer with purchasing, a callback to the garage about the company car, a demo for a new customer, sending the offer):

```
mkdir -p ~/notizen
```

```
cat > ~/notizen/heute.txt <<'EOF'
09:00 Abstimmung Angebot mit dem Einkauf
11:00 Rückruf Werkstatt wegen Firmenwagen
14:30 Demo bei einem Neukunden, Raum 2.14
17:00 Angebot an den Kunden schicken
EOF
```

### 3. Save the routine

Save these lines as `~/notizen/morgenzettel.py`. The two checks at the top run without a model: is the source there? Is it less than 24 hours old? Only then does the script ask qwen3:1.7b. Its system prompt asks for exactly three short bullet points in German.

```
import json, os, sys, time, urllib.request

QUELLE = os.path.expanduser(sys.argv[1] if len(sys.argv) > 1 else "~/notizen/heute.txt")
ZIEL = os.path.expanduser("~/notizen/morgenzettel.txt")
MAX_ALTER_STUNDEN = 24

if not os.path.exists(QUELLE):
    print(f"Quelle fehlt: {QUELLE}"); sys.exit(1)
alter_h = (time.time() - os.path.getmtime(QUELLE)) / 3600
if alter_h > MAX_ALTER_STUNDEN:
    print(f"Quelle veraltet: {alter_h:.0f} Stunden alt, nichts erzeugt"); sys.exit(1)

text = open(QUELLE, encoding="utf-8").read()[:4000]
anfrage = {"model": "qwen3:1.7b", "stream": False, "think": False,
           "options": {"temperature": 0.2, "num_ctx": 4096},
           "messages": [{"role": "system", "content":
                         "Fasse Notizen in genau drei kurzen Stichpunkten auf Deutsch zusammen."},
                        {"role": "user", "content": text}]}
try:
    req = urllib.request.Request("http://localhost:11434/api/chat",
        json.dumps(anfrage).encode(), {"Content-Type": "application/json"})
    antwort = json.load(urllib.request.urlopen(req, timeout=100))["message"]["content"].strip()
except Exception as fehler:
    print(f"Modell nicht erreichbar: {fehler}"); sys.exit(1)

with open(ZIEL, "w", encoding="utf-8") as f:
    f.write(f"Morgenzettel vom {time.strftime('%d.%m.%Y %H:%M')} "
            f"(Quelle {alter_h:.1f} h alt)\n{antwort}\n")
print(f"geschrieben: {ZIEL}")
```

### 4. Run with a fresh calendar

Afterward, `echo` shows the run’s exit code.

```
python3 ~/notizen/morgenzettel.py ~/notizen/heute.txt; echo "Exit-Code frisch: $?"
```

```
cat ~/notizen/morgenzettel.txt
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
geschrieben: …/notizen/morgenzettel.txt
Exit-Code frisch: 0
Morgenzettel vom 25.11.2026 09:25 (Quelle 0.0 h alt)
- Abstimmung von Angebot im Rahmen des Einkaufs um 09:00
- Rückruf bei Werkstatt um 11:00 wegen Firmenwagens
- Demo bei Neukunden um 14:30 in Raum 2.14
```

The run took 5 seconds under load. „geschrieben“ means “written”. Three bullet points, as requested, but the fourth appointment at 5 p.m. got lost along the way. Even a fresh routine deserves a critical look.

### 5. Run with yesterday’s calendar

A copy of the file whose modification time `touch -A` sets back by 26 hours, like Lukas’s cache:

```
cp ~/notizen/heute.txt ~/notizen/gestern.txt && touch -A -260000 ~/notizen/gestern.txt
```

```
python3 ~/notizen/morgenzettel.py ~/notizen/gestern.txt; echo "Exit-Code gestern: $?"
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
Quelle veraltet: 26 Stunden alt, nichts erzeugt
Exit-Code gestern: 1
```

„Quelle veraltet: 26 Stunden alt, nichts erzeugt“ means “Source outdated: 26 hours old, nothing created”; in the next step, „Quelle fehlt“ means “Source missing.” The model wasn’t asked at all. Check with `ls -l ~/notizen`: `morgenzettel.txt` from step 4 is still there. Anyone who later simply sends this file without paying attention to the exit code sends the old note. That’s exactly what happened to Lukas.

### 6. Run without a calendar

```
python3 ~/notizen/morgenzettel.py ~/notizen/fehlt.txt; echo "Exit-Code fehlt: $?"
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
Quelle fehlt: …/notizen/fehlt.txt
Exit-Code fehlt: 1
```

### 7. Let the page check it

Paste the three lines with “Exit-Code” from steps 4 to 6.

The check needs JavaScript. Without it: correct is 0 for fresh (frisch) and 1 for yesterday (gestern) and missing (fehlt).

### 8. Schedule it as a service

So that the routine runs on its own every morning at 7:30, launchd, macOS’s service manager, gets a description file (plist). This command writes it and fills in your home folder for `$HOME`. Python is listed with its full path, because launchd starts services with a very short search path.

```
cat > ~/Library/LaunchAgents/de.beispiel.morgenzettel.plist <<EOF
<?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>de.beispiel.morgenzettel</string>
  <key>ProgramArguments</key><array>
    <string>/usr/bin/python3</string>
    <string>$HOME/notizen/morgenzettel.py</string>
  </array>
  <key>StartCalendarInterval</key>
  <dict><key>Hour</key><integer>7</integer><key>Minute</key><integer>30</integer></dict>
  <key>StandardOutPath</key><string>$HOME/notizen/routine.log</string>
  <key>StandardErrorPath</key><string>$HOME/notizen/routine.log</string>
</dict></plist>
EOF
```

Check whether the file is readable:

```
plutil -lint ~/Library/LaunchAgents/de.beispiel.morgenzettel.plist
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
…/de.beispiel.morgenzettel.plist: OK
```

On the test Mac, the same file was written and checked in a different location, but not loaded. Load it, which also schedules it:

```
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/de.beispiel.morgenzettel.plist
```

The next morning, `~/notizen/routine.log` shows what the run printed. If `heute.txt` was already older than 24 hours by then, you’ll read „Quelle veraltet: …, nichts erzeugt“ there. (Checked against the documentation, not run here)

Something to think about: what happens if the Mac was closed at 7:30? According to `man launchd.plist` (section StartCalendarInterval), launchd runs the job when the Mac next wakes up; it merges several missed runs into one. Does the 24-hour limit still fit then?

### 9. Clean up

Remove the service if you loaded it, and delete the plist: (Checked against the documentation, not run here)

```
launchctl bootout gui/$(id -u)/de.beispiel.morgenzettel
```

```
rm ~/Library/LaunchAgents/de.beispiel.morgenzettel.plist
```

Delete the exercise files; your own notes in `~/notizen` stay:

```
rm ~/notizen/morgenzettel.py ~/notizen/morgenzettel.txt ~/notizen/heute.txt ~/notizen/gestern.txt
```

```
ollama stop qwen3:1.7b
```

#### On the DGX Spark

The Spark runs DGX OS, a Linux based on Ubuntu 24.04. There’s no launchd there; the service manager on Linux is systemd. `morgenzettel.py` itself runs unchanged, as long as Ollama is reachable at `localhost:11434`. What’s more worthwhile there is a whole long-runner: the playbook “Run OpenClaw with a Local LLM” connects OpenClaw to a local model, through vLLM or through Ollama with qwen3.6:27b and 65,536 tokens of context. “Set Up Example NemoClaw Agents” comes with four ready-made applications, among them a morning news overview. When you try them, keep the same question in mind as here: what does the agent do when its source fails? (Checked against the documentation, not run here)

The exercise with diagrams for every part of the plist is in [chapter 17](https://llm.yoga/en/17-selbststaendige-agenten-3.md).
