innmind / cron
Helper to install crontabs on a machine
3.2.0
2023-09-23 09:53 UTC
Requires
- php: ~8.2
- innmind/immutable: ~4.2|~5.0
- innmind/server-control: ~4.1|~5.0
Requires (Dev)
- innmind/black-box: ~5.5
- innmind/coding-standard: ~2.0
- phpunit/phpunit: ~10.2
- vimeo/psalm: ~5.12
Suggests
- innmind/operating-system: To easily access to a machine control mechanism
This package is auto-updated.
Last update: 2024-10-23 12:25:54 UTC
README
Library to help manage crontabs of a machine
Important: to correctly use this library you must validate your code with vimeo/psalm
Installation
composer require innmind/cron
Usage
Insalling jobs
For the default user :
use Innmind\Cron\{ Crontab, Job, }; use Innmind\OperatingSystem\Factory; $os = Factory::build(); $install = Crontab::forConnectedUser( Job::of('* * * * * say hello'), Job::of('*/2 * * * * say world'), ); $install($os->control()); // this is the same as running "echo '* * * * * say hello' | crontab" in your terminal
For a specific user :
use Innmind\Cron\{ Crontab, Job, }; use Innmind\OperatingSystem\Factory; use Innmind\Server\Control\ScriptFailed; use Innmind\Immutable\{ Either, SideEffect, }; $os = Factory::build(); $install = Crontab::forUser( 'watev', Job::of('* * * * * say hello'), ); $install($os->control()); // Either<ScriptFailed, SideEffect> // this is the same as running "echo '* * * * * say hello' | crontab -u admin" in your terminal
Since this library rely on innmind/server-control
you can easily install a crontab on a remote server. For example installing a crontab for the user admin
on the server example.com
would be done like this :
use Innmind\Cron\{ Crontab, Job, }; use Innmind\OperatingSystem\Factory; use Innmind\Url\Url; $os = Factory::build(); $install = Crontab::forUser( 'admin', Job::of('* * * * * say hello'), ); $install( $os->remote()->ssh(Url::of('ssh://example.com')), );
Note: At the moment the library does not support adding comments and spaces in the crontab.
Reading a crontab
For the default user :
use Innmind\Cron\{ Read, Job, }; use Innmind\OperatingSystem\Factory; use Innmind\Immutable\{ Maybe, Sequence, }; $os = Factory::build(); $read = Read::forConnectedUser(); $jobs = $read($os->control()); // it will run "crontab -l" // $jobs is an instance of Maybe<Sequence<Job>>
For a specific user :
use Innmind\Cron\{ Read, Job, }; use Innmind\OperatingSystem\Factory; use Innmind\Immutable\{ Maybe, Sequence, }; $os = Factory::build(); $read = Read::forUser('watev'); $jobs = $read($os->control()); // it will run "crontab -u watev -l" // $jobs is an instance of Maybe<Sequence<Job>>
Note: At the moment comments and spaces are not listed in the $jobs
variable.