CronJobManager is a PHP class designed to simplify the management of cron jobs. This library allows you to programmatically add, remove, and check the existence of cron jobs within a crontab file. It also includes utilities to set a specific crontab as active and handle paths and files that do not yet exist.
- Add Cron Jobs: Easily add new cron jobs with specified time parameters.
- Remove Cron Jobs: Remove cron jobs based on their command.
- Check Existence: Check if a cron job already exists in the crontab.
- Set Active Crontab: Set a specific crontab file as active, with automatic directory and file creation if needed.
- Empty Crontab: Clear all entries from the current crontab.
- Remove Crontab: Completely remove the current crontab from the system.
- PHP: This library requires PHP 5.6 or later.
- Operating System: Linux or any other Unix-like system where cron is available.
- Permissions: The user running the PHP script must have appropriate permissions to access and modify crontab files.
To install this library, simply clone the repository and include the CronJobManager.php
file in your project:
git clone https://github.com/baaziznasser/php-cron-job-manager.git
Then include the class in your PHP file:
require_once 'path/to/Cron_job_manager.php';
Start by initializing the CronJobManager
class. You can optionally provide a path to the crontab file you want to manage.
$cronManager = new CronJobManager('/path/to/your/crontab/tmp/crontab.tmp');
You can set a specific crontab file as active. If the path or file does not exist, the method will create them.
$cronManager->setCrontabActive('/path/to/your/crontab/tmp/crontab.tmp');
To add a new cron job that runs every Sunday at 6:05 AM:
$cronManager->addCronJob('5', '6', '*', '*', '0', '/usr/bin/php /path/to/your/script.php');
To check if a specific cron job exists:
if ($cronManager->cronExists('/usr/bin/php /path/to/your/script.php')) {
echo "Cron job exists!";
} else {
echo "Cron job does not exist!";
}
To remove a cron job based on its command:
$cronManager->removeCronJob('/usr/bin/php /path/to/your/script.php');
To remove all cron jobs from the current crontab:
$cronManager->emptyCrontab();
To remove the current crontab from the system and set its path to null
:
$cronManager->removeCurrentCrontab();
- The user running the script must have permission to read, write, and execute commands related to crontab management.
- The PHP script must have file system permissions to create directories and files if they do not already exist.
This project is licensed under the MIT License. See the LICENSE file for more details.
This library was created by Nacer Baaziz. If you have any questions or suggestions, feel free to contact me through GitHub or at my email.
Happy coding! If you find this library useful, consider giving the repository a star 🌟.