Skip to content

randomn4me/crontab-to-launchd

Repository files navigation

crontab-to-launchd

Convert crontab expressions to macOS launchd plist format.

Features

  • Full crontab syntax support (wildcards, ranges, lists, steps)
  • Special strings (@reboot, @daily, @weekly, @monthly, @yearly, @annually)
  • Environment variable support
  • Generates valid launchd plist XML files
  • Comprehensive validation with macOS plutil

Installation

Using Nix

# Run directly
nix run github:randomn4me/crontab-to-launchd -- "0 2 * * * /usr/bin/backup.sh" com.example.backup

# Install to profile
nix profile install github:randomn4me/crontab-to-launchd

Using pip

pip install -r requirements.txt
python -m crontab_to_launchd.main "0 2 * * * /usr/bin/backup.sh" com.example.backup

Usage

Command Line

# Basic usage
crontab-to-launchd "0 2 * * * /usr/bin/backup.sh" com.example.backup

# Save to file
crontab-to-launchd "0 2 * * * /usr/bin/backup.sh" com.example.backup -o backup.plist

# Validate only
crontab-to-launchd "0 2 * * * /usr/bin/backup.sh" com.example.backup -v

# Special strings
crontab-to-launchd "@daily /usr/bin/daily.sh" com.example.daily

Python API

from crontab_to_launchd.parser import CrontabParser
from crontab_to_launchd.generator import LaunchdGenerator

# Parse crontab
parser = CrontabParser()
entry = parser.parse("0 2 * * * /usr/bin/backup.sh")

# Generate plist
generator = LaunchdGenerator()
xml = generator.generate(entry, "com.example.backup")
print(xml)

Architecture

Core Modules

  • parser.py: Parses crontab expressions into CrontabEntry dataclass
  • generator.py: Converts CrontabEntry to launchd plist XML
  • main.py: CLI interface

Module Dependencies

parser.py (foundation)
    ↓
generator.py (depends on parser.CrontabEntry)
    ↓
main.py (CLI, uses both)

Testing

# Run all tests
nix run .#test

# Or with pytest
pytest tests/ -v

Test Coverage

  • 83 total tests covering:
    • Crontab parser validation
    • Launchd XML generation
    • Field boundary validation
    • Complex pattern combinations
    • plutil validation (18 tests)
    • Real-world examples (28 patterns)

All generated plist files are validated with macOS plutil -lint to ensure compatibility with launchd.

Development

Nix Development Shell

nix develop

Provides Python 3, pytest, black, flake8, and all dependencies.

Project Structure

src/crontab_to_launchd/
├── __init__.py
├── parser.py       # Crontab parsing
├── generator.py    # Launchd XML generation
└── main.py         # CLI entry point

tests/
├── test_crontab_parser.py
├── test_launchd_generator.py
├── test_field_validation.py
├── test_comprehensive_patterns.py
└── test_plutil_validation.py

Examples

Every 5 minutes

crontab-to-launchd "*/5 * * * * /path/to/script.sh" com.example.every5min

Daily at 2 AM

crontab-to-launchd "0 2 * * * /path/to/backup.sh" com.example.backup

Weekdays at 9 AM

crontab-to-launchd "0 9 * * 1-5 /path/to/weekday.sh" com.example.weekday

Multiple specific hours

crontab-to-launchd "0 9,12,15,18 * * * /path/to/check.sh" com.example.check

License

MIT

About

Convert crontab expressions to macOS launchd plist format.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published