{"id":13828548,"url":"https://github.com/TiBeN/CrontabManager","last_synced_at":"2025-07-09T06:32:09.356Z","repository":{"id":12952106,"uuid":"15630266","full_name":"TiBeN/CrontabManager","owner":"TiBeN","description":"PHP library for GNU/Linux cron jobs management.","archived":false,"fork":false,"pushed_at":"2024-05-02T14:06:56.000Z","size":56,"stargazers_count":137,"open_issues_count":1,"forks_count":29,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-09-19T11:22:06.797Z","etag":null,"topics":["composer","cron","cron-jobs","crontab","linux","php","php-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TiBeN.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-01-04T10:24:04.000Z","updated_at":"2024-09-19T01:11:27.000Z","dependencies_parsed_at":"2023-01-13T17:13:13.515Z","dependency_job_id":"2a2a8cc5-b94e-4c83-a6c4-892a92fcdef0","html_url":"https://github.com/TiBeN/CrontabManager","commit_stats":{"total_commits":56,"total_committers":7,"mean_commits":8.0,"dds":0.3928571428571429,"last_synced_commit":"9eba6bde831ab715eb6df5d97897df12c6bd6d8a"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TiBeN%2FCrontabManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TiBeN%2FCrontabManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TiBeN%2FCrontabManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TiBeN%2FCrontabManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TiBeN","download_url":"https://codeload.github.com/TiBeN/CrontabManager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225492420,"owners_count":17482869,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","cron","cron-jobs","crontab","linux","php","php-library"],"created_at":"2024-08-04T09:02:51.742Z","updated_at":"2024-11-20T08:30:41.397Z","avatar_url":"https://github.com/TiBeN.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"CrontabManager\n==============\n\nPHP library to manage programmatically GNU/Linux cron jobs.\n\nIt enables you to :\n\n-   Deal with your cron jobs in PHP.\n-   Create new Cron jobs.\n-   Update existing cron jobs.\n-   Manage cron jobs of others users than runtime user using some sudo\n    tricks (see below).\n\nRequirements\n------------\n\n-   PHP 5.3+\n-   `crontab` command-line utility (should be already installed in your\n    distribution).\n-   `sudo`, if you want to manage crontab of another user than runtime\n    user without running into right issues (see below)\n\nInstallation\n------------\n\nThe library can be installed using Composer.\n\n    composer require tiben/crontab-manager ~1.0\n\nUsage\n-----\n\nThe library is composed of three classes:\n\n-   `CrontabJob` is an entity class which represents a cron job.\n-   `CrontabRepository` is used to persist/retrieve your cron jobs.\n-   `CrontabAdapter` handles cron jobs persistance in the crontab.\n\n### Instantiate the repository\n\nIn order to work, the CrontabRepository needs an instance of\nCrontabAdapter.\n\n``` {.php}\n$crontabRepository = new CrontabRepository(new CrontabAdapter());\n```\n\n### Create new Job and persist it into the crontab\n\nSuppose you want to create a new job which consists of launching the\ncommand \"df \u0026gt;\u0026gt; /tmp/df.log\" every day at 23:30. You can do it in\ntwo ways.\n\n-   In Pure oo way :\n\n    ``` {.php}\n    $crontabJob = new CrontabJob();\n    $crontabJob\n        -\u003esetMinutes(30)\n        -\u003esetHours(23)\n        -\u003esetDayOfMonth('*')\n        -\u003esetMonths('*')\n        -\u003esetDayOfWeek('*')\n        -\u003esetTaskCommandLine('df \u003e\u003e /tmp/df.log')\n        -\u003esetComments('Logging disk usage'); // Comments are persisted in the crontab\n    ```\n\n-   From raw cron syntax string using a factory method :\n\n    ``` {.php}\n    $crontabJob = CrontabJob::createFromCrontabLine('30 23 * * * df \u003e\u003e /tmp/df.log');\n    ```\n\nYou can now add your new cronjob in the crontab repository and persist\nall changes to the crontab.\n\n``` {.php}\n$crontabRepository-\u003eaddJob($crontabJob);\n$crontabRepository-\u003epersist();\n```\n\n### Find a specific cron job from the crontab repository and update it\n\nSuppose we want to modify the hour of an already existing cronjob.\nFinding existings jobs is done using some regular expressions. The regex\nis applied to the entire crontab line.\n\n``` {.php}\n$results = $crontabRepository-\u003efindJobByRegex('/Logging\\ disk\\ usage/');\n$crontabJob = $results[0];\n$crontabJob-\u003esetHours(21);\n$crontabRepository-\u003epersist();\n```\n\n### Remove a cron job from the crontab\n\nYou can remove a job like this :\n\n``` {.php}\n$results = $crontabRepository-\u003efindJobByRegex('/Logging\\ disk\\ usage/');\n$crontabJob = $results[0];\n$crontabRepository-\u003eremoveJob($crontabJob);\n$crontabRepository-\u003epersist();\n```\n\nNote: Since cron jobs are internally matched by reference, they must be\npreviously obtained from the repository or previously added.\n\n### Work with the crontab of another user than runtime user\n\nThis feature allows you to manage the crontab of another user than the\nuser who launched the runtime. This can be useful when the runtime user\nis `www-data` but the owner of the crontab you want to edit is your own\nlinux user account.\n\nTo do this, simply pass the username of the crontab owner as parameter\nof the CrontabAdapter constructor. Suppose you are `www-data` and you\nwant to edit the crontab of user `bobby`:\n\n``` {.php}\n$crontabAdapter = new CrontabAdapter('bobby');\n$crontabRepository = new CrontabRepository($crontabAdapter);\n```\n\nUsing this way you will propably run into user rights issues. This can\nbe handled by editing your sudoers file using 'visudo'.\\\nIf you want to allow user `www-data` to edit the crontab of user\n`bobby`, add this line:\n\n    www-data        ALL=(bobby) NOPASSWD: /usr/bin/crontab\n\nwhich tells sudo not to ask for password when user `www-data` calls\n`crontab` as user `bobby` using `sudo`\n\nNow, you can access to the crontab of user `bobby` like this :\n\n``` {.php}\n$crontabAdapter = new CrontabAdapter('bobby', true);\n$crontabRepository = new CrontabRepository($crontabAdapter);\n```\n\nNote the second parameter `true` of the CrontabAdapter constructor call.\nThis boolean tells the CrontabAdapter to use `sudo` internally when\ncalling `crontab`.\n\n### Enable or disable a cron job\n\nYou can enable or disable your cron jobs by using the `setEnabled()`\nmethod of a CronJob object accordingly :\n\n``` {.php}\n$crontabJob-\u003esetEnabled(false);\n```\n\nThis will prepend your cron job with a `#` in your\ncrontab when persisting it.\n\n### Write your own adapter\nAdditionally, if you cannot read another user's crontabs or if you are on a distributed architecture where crons are not \nrun on the machine executing the jobs, you can create any other Adapter for your architecture \nby implementing the `CrontabAdapterInterface`.\n\nYou can then instantiate the `CrontabRepository` with your adapter.\n\nUnit tests\n----------\n\nTests have been written using PHPUnit and require version 5.3+. To\nexecute tests:\n\n    $ phpunit \u003ccrontab-library-path\u003e/tests\n\nIf you installed the library using Composer and installed\ndev-dependencies you can execute them using included PHPUnit as\ndependency:\n\n    $ ./vendor/bin/phpunit \u003ccrontab-library-path\u003e/tests\n\nContributions\n-------------\n\n... are welcome :)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTiBeN%2FCrontabManager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTiBeN%2FCrontabManager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTiBeN%2FCrontabManager/lists"}