# GenericAgent Scheduler Cron Integration: Automated Task Execution Explained

> Discover how GenericAgent scheduler integrates with cron for automated task execution. Learn about its Python-based daemon and autonomous agent triggering.

- Repository: [LJQ/GenericAgent](https://github.com/lsdefine/GenericAgent)
- Tags: how-to-guide
- Published: 2026-04-16

---

**TLDR:** GenericAgent implements a Python-based scheduler in [`reflect/scheduler.py`](https://github.com/lsdefine/GenericAgent/blob/main/reflect/scheduler.py) that mimics a lightweight cron daemon by polling JSON task definitions every `INTERVAL` seconds and triggering autonomous agent execution when cron-style time conditions are satisfied.

The `lsdefine/GenericAgent` repository provides a **Python-based scheduler** that bridges traditional cron workflows with autonomous AI task execution. By running as a *reflect script* via the `--reflect` CLI flag, the scheduler continuously monitors the `sche_tasks/` directory for job definitions and evaluates them against cron-style timing rules. This design allows you to automate GenericAgent's toolset and memory system using familiar time-based triggers without modifying the core agent logic.

## How the Scheduler Core Works (agentmain.py)

The scheduler operates through a periodic "tick" mechanism managed by [`agentmain.py`](https://github.com/lsdefine/GenericAgent/blob/main/agentmain.py). When launched with `python launch.pyw --reflect reflect/scheduler.py`, the loader imports the script and enters a continuous loop that sleeps for the default `INTERVAL` of 5 seconds before calling `scheduler.check()`【/cache/repos/github.com/lsdefine/GenericAgent/main/agentmain.py#L14-L22】.

This architecture creates a lightweight daemon that:

- Maintains the GenericAgent process in memory for rapid task execution
- Re-evaluates task conditions every 5 seconds without system cron overhead
- Automatically reloads if the script file modification time changes

## Cron-Style Task Configuration (sche_tasks/)

Tasks are defined as JSON files in the `sche_tasks/` directory. Each file specifies scheduling parameters that mirror traditional cron functionality while adding AI-specific execution controls.

Example from [`sche_tasks/example.json`](https://github.com/lsdefine/GenericAgent/blob/main/sche_tasks/example.json):

```json
{
  "enabled": true,
  "repeat": "daily",
  "schedule": "08:30",
  "max_delay_hours": 6,
  "prompt": "请每日 08:30 检查股票行情，并发送报告至微信。"
}

```

Key fields:

- **repeat**: Supports `"once"`, `"daily"`, `"weekday"`, `"weekly"`, `"monthly"`, or intervals like `"every_2h"`
- **schedule**: HH:MM format (24-hour clock)
- **max_delay_hours**: Defines an execution window to prevent stale job accumulation

## Task Discovery and Execution Logic (reflect/scheduler.py)

The `check()` function in [`reflect/scheduler.py`](https://github.com/lsdefine/GenericAgent/blob/main/reflect/scheduler.py) (lines 76-130) implements the cron-style evaluation engine. For each task file, it parses the scheduled time and applies multiple filtering layers:

```python
now = datetime.now()
h, m = map(int, sched.split(':'))

# Time window validation

max_delay = task.get('max_delay_hours', DEFAULT_MAX_DELAY)
if (now_minutes - (h*60 + m)) > max_delay * 60:
    continue

# Cooldown enforcement to prevent drift

last = _last_run(tid, done_files)
if last and (now - last) < _parse_cooldown(repeat):
    continue

# Trigger execution

_logger.info(f'TRIGGER {tid} …')
return f'[定时任务] {tid}\n…{prompt}…'

```

When conditions are met, `check()` returns a formatted prompt string that the main loop treats as a user request, feeding it directly into the autonomous agent execution pipeline with full access to tools and memory.

## OS Cron Integration Methods

GenericAgent provides two approaches to ensure continuous scheduling:

**Reflect Script Mode (Continuous):**

Run the scheduler as a persistent process:

```bash
python launch.pyw --reflect reflect/scheduler.py

```

This maintains the agent state and executes tasks immediately when due.

**System Cron Delegation (Fault-Tolerant):**

For production reliability, combine the internal scheduler with OS cron by creating a crontab entry that restarts the process periodically:

```cron
*/5 * * * * cd /path/to/GenericAgent && \
    /usr/bin/python3 launch.pyw --reflect reflect/scheduler.py \
    >> /var/log/genericagent-scheduler.log 2>&1

```

The internal 5-second `INTERVAL` ensures immediate task checking upon startup, while OS cron guarantees recovery from crashes.

## Built-in L4 Archive Maintenance

Every 12 hours, the scheduler automatically triggers an internal **L4 archive cron** job that compresses finished session archives. This silent maintenance task runs alongside user-defined schedules, ensuring the memory system remains optimized without manual intervention.

## Summary

- GenericAgent's scheduler in [`reflect/scheduler.py`](https://github.com/lsdefine/GenericAgent/blob/main/reflect/scheduler.py) provides cron-style automation through a Python-based polling mechanism
- Task definitions reside in `sche_tasks/` as JSON files supporting daily, weekly, and custom interval triggers
- The `check()` function evaluates time windows, max delays, and cooldown periods before returning execution prompts to the agent loop
- Run continuously via `--reflect` flag or delegate to OS cron for fault-tolerant operation
- Built-in L4 archive compression maintains system health every 12 hours automatically

## Frequently Asked Questions

### How does GenericAgent's scheduler differ from Linux cron?

GenericAgent's scheduler is a Python module that runs within the agent's execution loop, allowing tasks to access the full memory system and toolset. While Linux cron triggers external processes, GenericAgent's scheduler feeds prompts directly into the autonomous agent loop via [`agentmain.py`](https://github.com/lsdefine/GenericAgent/blob/main/agentmain.py), maintaining session context between executions.

### What file format does GenericAgent use for scheduled tasks?

Tasks are stored as JSON files in the `sche_tasks/` directory. Each file must include `enabled`, `repeat`, `schedule` (HH:MM), and `prompt` fields, with optional `max_delay_hours` to define execution windows.

### Can I run GenericAgent tasks without keeping the process running continuously?

Yes. You can create a system cron entry that launches `python launch.pyw --reflect reflect/scheduler.py` at fixed intervals (e.g., every 5 minutes). The script checks for due tasks immediately upon startup and executes them before exiting, though continuous operation provides better performance and memory persistence.

### How does the scheduler prevent tasks from running multiple times?

The `check()` function tracks the last execution time in `done_files` and enforces cooldown periods via `_parse_cooldown()`. If a task's repeat interval hasn't elapsed since the last run, the scheduler skips execution even if the scheduled time has passed.