Skip to content

Repository files navigation

Native Cron

This package helps you to manage and modify your native *nix crontabs.

See also mintware-de/native-cron-bundle if you're looking for a Symfony bundle.

Features

  • View / add / edit / delete system cron jobs
  • View / add / edit / delete drop in cron jobs
  • View / add / edit / delete user cron jobs
  • Fully tested (except the adapter for native file system functions)
  • Flexible / you can easily replace any important part of the package

Installation

composer require mintware-de/native-cron

Usage

<?phpdeclare(strict_types=1);
useMintwareDe\NativeCron\Content\BlankLine;
useMintwareDe\NativeCron\Content\CommentLine;
useMintwareDe\NativeCron\Content\CronJobLine;
useMintwareDe\NativeCron\CrontabManager;
useMintwareDe\NativeCron\Filesystem\DarwinCrontabFileLocator;
useMintwareDe\NativeCron\Filesystem\FileHandler;
require_once__DIR__.'/vendor/autoload.php';
// Create a new crontab manager$manager = newCrontabManager(
// with a file locator for macOS; Use DebianCrontabFileLocator for debian based distrosnewDarwinCrontabFileLocator(),
// and a default file handlernewFileHandler(),
);
// Read the crontab for the user max$crontab = $manager->readUserCrontab('max');
// Display the current content of the crontabecho$crontab->build();
$crontab// Add a new cronjob
->add(newCronJobLine('* * * * * echo "Hello World" >> /tmp/mylog'))
// Remove all comments and blank lines
->removeWhere(fn ($line) => $lineinstanceof CommentLine || $lineinstanceof BlankLine);
// Display the new content of the crontabecho$crontab->build();
// Write the crontab for the user max// Keep in mind that reading or writing crontab files may require higher user privileges.$manager->writeUserCrontab($crontab, 'max');

Compatibility matrix

FeatureLinuxmacOSWin
System Cron jobsYesYesNo
User Cron jobsYesYesNo
Drop-In Cron jobsYesNoNo

Supported platforms

At the moment are Debian based distros and macOS supported. If you need to add support for a different platform, take a look at the CrontabFileLocatorInterface and implement it for your platform.

About

Manage native unix cron jobs with PHP

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages