A clean and easy-to-use PHP package for managing cron jobs on Unix-like systems (Linux, macOS, etc.). This package allows you to programmatically add, update, remove, and list cron jobs with support for both standard cron expressions and human-readable schedules.
- ✅ List all cron jobs with standard cron times and human-readable descriptions
- ✅ Add new cron jobs using standard cron format or human-readable expressions
- ✅ Update existing cron jobs
- ✅ Remove cron jobs
- ✅ Windows detection with appropriate exception handling
- ✅ Clean, object-oriented API
- ✅ Comprehensive test coverage
- PHP 8.0 or higher
- Unix-like operating system (Linux, macOS, etc.)
crontabcommand available in PATH- Windows is not supported (throws
UnsupportedOSException)
composer require zpmlabs/php-cron-manager<?php
require 'vendor/autoload.php';
use ZPMLabs\CronManager\CronManager;
use ZPMLabs\CronManager\CronJob;
// Create a new CronManager instance
$manager = new CronManager();
// List all cron jobs
$crons = $manager->listCrons();
foreach ($crons as $cron) {
echo "Schedule: " . $cron->getSchedule() . "\n";
echo "Command: " . $cron->getCommand() . "\n";
echo "Comment: " . ($cron->getComment() ?? 'N/A') . "\n";
echo "---\n";
}$crons = $manager->listCronsWithHumanReadable();
foreach ($crons as $cron) {
echo "Schedule: " . $cron['schedule'] . "\n";
echo "Human-readable: " . $cron['human_readable'] . "\n";
echo "Command: " . $cron['job']->getCommand() . "\n";
echo "---\n";
}// Add a cron job that runs every minute
$manager->addCron('every minute', '/usr/bin/php /path/to/script.php', 'Runs every minute');
// Add a cron job that runs every hour
$manager->addCron('every hour', '/usr/bin/php /path/to/script.php', 'Hourly task');
// Add a cron job that runs every day at midnight
$manager->addCron('every day', '/usr/bin/backup.sh', 'Daily backup');
// Add a cron job with custom interval
$manager->addCron('every 5 minutes', '/usr/bin/php /path/to/script.php');
$manager->addCron('every 2 hours', '/usr/bin/php /path/to/script.php');
$manager->addCron('every 3 days', '/usr/bin/php /path/to/script.php');// Standard cron format: minute hour day month weekday
$manager->addCron('0 0 * * *', '/usr/bin/php /path/to/script.php', 'Daily at midnight');
$manager->addCron('*/15 * * * *', '/usr/bin/php /path/to/script.php', 'Every 15 minutes');
$manager->addCron('0 9 * * 1', '/usr/bin/php /path/to/script.php', 'Every Monday at 9 AM');$cronJob = new CronJob(
'0 0 * * *',
'/usr/bin/php /path/to/script.php',
'Daily backup'
);
$manager->addCron($cronJob);// Get all cron jobs
$crons = $manager->listCrons();
// Update the first cron job (index 0)
$manager->updateCron(0, 'every hour', '/usr/bin/php /path/to/new_script.php', 'Updated job');
// Or update using CronJob object
$updatedJob = new CronJob('0 */2 * * *', '/usr/bin/php /path/to/script.php', 'Every 2 hours');
$manager->updateCron(0, $updatedJob);// Remove a specific cron job by index
$crons = $manager->listCrons();
$manager->removeCron(0); // Remove the first cron job
// Remove all cron jobs
$manager->removeAllCrons();try {
$manager = new CronManager();
// ... use the manager
} catch (\ZPMLabs\CronManager\Exception\UnsupportedOSException $e) {
echo "Error: " . $e->getMessage() . "\n";
// Handle Windows case - perhaps use Task Scheduler API instead
}The following human-readable expressions are supported:
every minute→* * * * *every hour→0 * * * *every day→0 0 * * *every week→0 0 * * 0every month→0 0 1 * *every year→0 0 1 1 *every N minutes→*/N * * * *(where N is 1-59)every N hours→0 */N * * *(where N is 1-23)every N days→0 0 */N * *(where N is 1-31)
You can also use standard cron expressions directly, which will be validated and used as-is.
__construct(?string $crontabPath = null)- Create a new CronManager instancelistCrons(bool $includeHumanReadable = false): array- List all cron jobslistCronsWithHumanReadable(): array- List all cron jobs with human-readable schedulesaddCron($scheduleOrJob, ?string $command = null, ?string $comment = null): bool- Add a new cron jobupdateCron(int $index, $scheduleOrJob, ?string $command = null, ?string $comment = null): bool- Update an existing cron jobremoveCron(int $index): bool- Remove a cron job by indexremoveAllCrons(): bool- Remove all cron jobs
__construct(string $schedule, string $command, ?string $comment = null)- Create a new CronJobgetSchedule(): string- Get the cron schedulegetCommand(): string- Get the command to executegetComment(): ?string- Get the optional commentsetSchedule(string $schedule): void- Set the cron schedulesetCommand(string $command): void- Set the commandsetComment(?string $comment): void- Set the commenttoCrontabLine(): string- Convert to crontab line formatstatic fromCrontabLine(string $line): self- Create from crontab line
static parse(string $expression): string- Parse human-readable expression to cron formatstatic isValidCronExpression(string $expression): bool- Validate cron expressionstatic toHumanReadable(string $cronExpression): string- Convert cron expression to human-readable
Run the test suite:
composer testOr using PHPUnit directly:
vendor/bin/phpunit-
Windows Support: This package does not support Windows. Windows uses Task Scheduler instead of cron. Attempting to use this package on Windows will throw an
UnsupportedOSException. -
Permissions: The script must have permission to modify the user's crontab. Typically, this means running as the user whose crontab you want to modify.
-
Backup: Always backup your crontab before making programmatic changes. You can do this manually with:
crontab -l > crontab_backup.txt -
Index-based Operations: When updating or removing cron jobs, use the index from
listCrons(). The index is 0-based and corresponds to the order in which cron jobs appear in the crontab.
MIT
ZPMLabs