71

GitHub - TiBeN/CrontabManager: PHP library for GNU/Linux cron jobs management.

 5 years ago
source link: https://github.com/TiBeN/CrontabManager
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

CrontabManager

PHP library for GNU/Linux cron jobs management.

Some features:

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

Requirements:

  • PHP 5.3+
  • crontab command-line utility (installed by default on common Linux distribution)
  • sudo, if you want to manage crontab of another user than runtime user without running into right issues (see below)

Installation:

The library can be installed using Composer.

composer require tiben/crontab-manager ~1.0

Usage:

The library is composed of three classes:

  • CrontabJob is an entity class that represents a cron job.
  • CrontabRepository is used to persist and or retrieve your cron jobs.
  • CrontabAdapter handles cron jobs persistance in the crontab.

Instanciate 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

Suppose you want to create an new job which consists of launching the command df >> /tmp/df.log every day at 23:30. There is two ways this can be done:

  • In Pure OO way :

    $crontabJob = new CrontabJob();
    $crontabJob->minutes = '30';
    $crontabJob->hours = '23';
    $crontabJob->dayOfMonth = '*';
    $crontabJob->months = '*';
    $crontabJob->dayOfWeek = '*';
    $crontabJob->taskCommandLine = 'df >> /tmp/df.log';
    $crontabJob->comments = '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 cron job 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

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

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

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

This feature allows you to manage the crontab of another (unix) user than the user who launched the php runtime. This can be useful when the runtime user is www-data — common when PHP is used along with a webserver — 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 a 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);

In this context 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

You can enable or disable your cron jobs by setting the enabled attribute of a CronJob object accordingly :

$crontabJob->enabled = false;

This will have the effect to prepend your cron job by a "#" in your crontab when persisting it.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK