tiben/crontab-manager
Composer 安装命令:
composer require tiben/crontab-manager
包简介
A library for managing linux cron jobs.
关键字:
README 文档
README
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
- PHP 5.3+
crontabcommand-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
The library can be installed using Composer.
composer require tiben/crontab-manager ~1.0
Usage
The library is composed of three classes:
CrontabJobis an entity class which represents a cron job.CrontabRepositoryis used to persist/retrieve your cron jobs.CrontabAdapterhandles cron jobs persistance in the crontab.
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
Suppose you want to create a new job which consists of launching the command "df >> /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
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
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 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
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
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
Tests have been written using PHPUnit and require version 5.3+. To execute tests:
$ phpunit <crontab-library-path>/tests
If you installed the library using Composer and installed dev-dependencies you can execute them using included PHPUnit as dependency:
$ ./vendor/bin/phpunit <crontab-library-path>/tests
Contributions
... are welcome :)
tiben/crontab-manager 适用场景与选型建议
tiben/crontab-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 158.59k 次下载、GitHub Stars 达 138, 最近一次更新时间为 2014 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「crontab」 「scheduling」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tiben/crontab-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tiben/crontab-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tiben/crontab-manager 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a possibility to auto-setup crontabs required for project based on configuration file.
symfony bundle for jobbyphp/jobby
Scheduling extension for Yii2 framework
Task scheduling extension for MoonShine
Yii2 scheduler module
CRON scheduling extension for Yii2 framework
统计信息
- 总下载量: 158.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 140
- 点击次数: 29
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2014-01-11