PHPackages                             tiben/crontab-manager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. tiben/crontab-manager

ActiveLibrary

tiben/crontab-manager
=====================

A library for managing linux cron jobs.

v1.4.0(3y ago)138144.3k↓19.8%299Apache-2.0PHPPHP &gt;= 5.3

Since Jan 11Pushed 1y ago13 watchersCompare

[ Source](https://github.com/TiBeN/CrontabManager)[ Packagist](https://packagist.org/packages/tiben/crontab-manager)[ RSS](/packages/tiben-crontab-manager/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (1)Versions (10)Used By (9)

CrontabManager
==============

[](#crontabmanager)

PHP library to manage programmatically GNU/Linux cron jobs.

It enables you to :

- Deal with your cron jobs in PHP.
- Create new Cron jobs.
- Update existing cron jobs.
- Manage cron jobs of others users than runtime user using some sudo tricks (see below).

Requirements
------------

[](#requirements)

- PHP 5.3+
- `crontab` command-line utility (should be already installed in your distribution).
- `sudo`, if you want to manage crontab of another user than runtime user without running into right issues (see below)

Installation
------------

[](#installation)

The library can be installed using Composer.

```
composer require tiben/crontab-manager ~1.0

```

Usage
-----

[](#usage)

The library is composed of three classes:

- `CrontabJob` is an entity class which represents a cron job.
- `CrontabRepository` is used to persist/retrieve your cron jobs.
- `CrontabAdapter` handles cron jobs persistance in the crontab.

### Instantiate the repository

[](#instantiate-the-repository)

In order to work, the CrontabRepository needs an instance of CrontabAdapter.

```
$crontabRepository = new CrontabRepository(new CrontabAdapter());

```

### Create new Job and persist it into the crontab

[](#create-new-job-and-persist-it-into-the-crontab)

Suppose you want to create a new job which consists of launching the command "df &gt;&gt; /tmp/df.log" every day at 23:30. You can do it in two ways.

- In Pure oo way :

    ```
    $crontabJob = new CrontabJob();
    $crontabJob
        ->setMinutes(30)
        ->setHours(23)
        ->setDayOfMonth('*')
        ->setMonths('*')
        ->setDayOfWeek('*')
        ->setTaskCommandLine('df >> /tmp/df.log')
        ->setComments('Logging disk usage'); // Comments are persisted in the crontab

    ```
- From raw cron syntax string using a factory method :

    ```
    $crontabJob = CrontabJob::createFromCrontabLine('30 23 * * * df >> /tmp/df.log');

    ```

You can now add your new cronjob in the crontab repository and persist all changes to the crontab.

```
$crontabRepository->addJob($crontabJob);
$crontabRepository->persist();

```

### Find a specific cron job from the crontab repository and update it

[](#find-a-specific-cron-job-from-the-crontab-repository-and-update-it)

Suppose we want to modify the hour of an already existing cronjob. Finding existings jobs is done using some regular expressions. The regex is applied to the entire crontab line.

```
$results = $crontabRepository->findJobByRegex('/Logging\ disk\ usage/');
$crontabJob = $results[0];
$crontabJob->setHours(21);
$crontabRepository->persist();

```

### Remove a cron job from the crontab

[](#remove-a-cron-job-from-the-crontab)

You can remove a job like this :

```
$results = $crontabRepository->findJobByRegex('/Logging\ disk\ usage/');
$crontabJob = $results[0];
$crontabRepository->removeJob($crontabJob);
$crontabRepository->persist();

```

Note: Since cron jobs are internally matched by reference, they must be previously obtained from the repository or previously added.

### Work with the crontab of another user than runtime user

[](#work-with-the-crontab-of-another-user-than-runtime-user)

This feature allows you to manage the crontab of another user than the user who launched the runtime. This can be useful when the runtime user is `www-data` but the owner of the crontab you want to edit is your own linux user account.

To do this, simply pass the username of the crontab owner as parameter of the CrontabAdapter constructor. Suppose you are `www-data` and you want to edit the crontab of user `bobby`:

```
$crontabAdapter = new CrontabAdapter('bobby');
$crontabRepository = new CrontabRepository($crontabAdapter);

```

Using this way you will propably run into user rights issues. This can be handled by editing your sudoers file using 'visudo'.
If you want to allow user `www-data` to edit the crontab of user `bobby`, add this line:

```
www-data        ALL=(bobby) NOPASSWD: /usr/bin/crontab

```

which tells sudo not to ask for password when user `www-data` calls `crontab` as user `bobby` using `sudo`

Now, you can access to the crontab of user `bobby` like this :

```
$crontabAdapter = new CrontabAdapter('bobby', true);
$crontabRepository = new CrontabRepository($crontabAdapter);

```

Note the second parameter `true` of the CrontabAdapter constructor call. This boolean tells the CrontabAdapter to use `sudo` internally when calling `crontab`.

### Enable or disable a cron job

[](#enable-or-disable-a-cron-job)

You can enable or disable your cron jobs by using the `setEnabled()`method of a CronJob object accordingly :

```
$crontabJob->setEnabled(false);

```

This will prepend your cron job with a `#` in your crontab when persisting it.

### Write your own adapter

[](#write-your-own-adapter)

Additionally, if you cannot read another user's crontabs or if you are on a distributed architecture where crons are not run on the machine executing the jobs, you can create any other Adapter for your architecture by implementing the `CrontabAdapterInterface`.

You can then instantiate the `CrontabRepository` with your adapter.

Unit tests
----------

[](#unit-tests)

Tests have been written using PHPUnit and require version 5.3+. To execute tests:

```
$ phpunit /tests

```

If you installed the library using Composer and installed dev-dependencies you can execute them using included PHPUnit as dependency:

```
$ ./vendor/bin/phpunit /tests

```

Contributions
-------------

[](#contributions)

... are welcome :)

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity50

Moderate usage in the ecosystem

Community31

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 82.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~485 days

Recently: every ~610 days

Total

8

Last Release

1112d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e4a0d28b55c10cc16d62c3a4cfb5f9d095c1ee9d5b13778c4eb8c3addb62b06a?d=identicon)[TiBeN](/maintainers/TiBeN)

---

Top Contributors

[![TiBeN](https://avatars.githubusercontent.com/u/910115?v=4)](https://github.com/TiBeN "TiBeN (42 commits)")[![samnela](https://avatars.githubusercontent.com/u/1852108?v=4)](https://github.com/samnela "samnela (2 commits)")[![Menelion](https://avatars.githubusercontent.com/u/595597?v=4)](https://github.com/Menelion "Menelion (2 commits)")[![TomLorenzi](https://avatars.githubusercontent.com/u/57677046?v=4)](https://github.com/TomLorenzi "TomLorenzi (1 commits)")[![bsattelb](https://avatars.githubusercontent.com/u/6740015?v=4)](https://github.com/bsattelb "bsattelb (1 commits)")[![wenqinruan](https://avatars.githubusercontent.com/u/5706241?v=4)](https://github.com/wenqinruan "wenqinruan (1 commits)")[![Ph0tonic](https://avatars.githubusercontent.com/u/5320541?v=4)](https://github.com/Ph0tonic "Ph0tonic (1 commits)")[![SergioMendolia](https://avatars.githubusercontent.com/u/4610317?v=4)](https://github.com/SergioMendolia "SergioMendolia (1 commits)")

---

Tags

composercroncron-jobscrontablinuxphpphp-libraryschedulingcrontab

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tiben-crontab-manager/health.svg)

```
[![Health](https://phpackages.com/badges/tiben-crontab-manager/health.svg)](https://phpackages.com/packages/tiben-crontab-manager)
```

###  Alternatives

[omnilight/yii2-scheduling

Scheduling extension for Yii2 framework

3181.0M7](/packages/omnilight-yii2-scheduling)[mult1mate/cron-manager

Flexible cron tasks manager for MVC-type applications

40338.5k3](/packages/mult1mate-cron-manager)[orisai/scheduler

Cron job scheduler - with locks, parallelism and more

4037.1k4](/packages/orisai-scheduler)[laravel-admin-ext/scheduling

Task scheduling extension for laravel-admin

93247.1k6](/packages/laravel-admin-ext-scheduling)[workerman/crontab

A crontab written in PHP based on workerman

70164.2k58](/packages/workerman-crontab)[xfra35/f3-cron

Job scheduling for the PHP Fat-Free Framework

73107.5k](/packages/xfra35-f3-cron)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
