### Crunz Configuration File Example Source: https://github.com/crunzphp/crunz/blob/3.10/README.md This is an example of the Crunz configuration file in YAML format. Modify these settings to customize Crunz behavior, such as task source directories, logging, and email notifications. ```yaml # Crunz Configuration Settings # This option defines where the task files and # directories reside. # The path is relative to the project's root directory, # where the Crunz is installed (Trailing slashes will be ignored). source: tasks # The suffix is meant to target the task files inside the ":source" directory. # Please note if you change this value, you need # to make sure all the existing tasks files are renamed accordingly. suffix: Tasks.php # Timezone is used to calculate task run time # This option is very important and not setting it is deprecated # and will result in exception in 2.0 version. timezone: ~ # This option define which timezone should be used for log files # If false, system default timezone will be used # If true, the timezone in config file that is used to calculate task run time will be used timezone_log: false # By default the errors are not logged by Crunz # You may set the value to true for logging the errors log_errors: false # This is the absolute path to the errors' log file # You need to make sure you have the required permission to write to this file though. errors_log_file: # By default the output is not logged as they are redirected to the # null output. # Set this to true if you want to keep the outputs log_output: false # This is the absolute path to the global output log file # The events which have dedicated log files (defined with them), won't be # logged to this file though. output_log_file: # By default line breaks in logs aren't allowed. # Set the value to true to allow them. log_allow_line_breaks: false # By default empty context arrays are shown in the log. # Set the value to true to remove them. log_ignore_empty_context: false # This option determines whether the output should be emailed or not. email_output: false # This option determines whether the error messages should be emailed or not. email_errors: false # Global Swift Mailer settings # mailer: # Possible values: smtp, mail, and sendmail transport: smtp recipients: sender_name: sender_email: # SMTP settings # smtp: host: port: username: password: encryption: ``` -------------------------------- ### Get Crunz Help Information Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Run the --help command to view all available options and arguments for the Crunz CLI. ```bash vendor/bin/crunz --help ``` -------------------------------- ### Execute a Script with Options Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Run an external script using `run()` and pass options as an associative array. This example executes a backup script with a destination option. ```php run(PHP_BINARY . ' backup.php', ['--destination' => 'path/to/destination']); $task ->everyMinute() ->description('Copying the project directory'); return $schedule; ``` -------------------------------- ### Get Crunz make:task Help Information Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Run the make:task --help command to see all available options and their default values for generating tasks. ```bash vendor/bin/crunz make:task --help ``` -------------------------------- ### Schedule Task Weekly on Tuesday at 09:00 Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Example demonstrating `weeklyOn()` for a specific day (Tuesday, day 2) and time (09:00). ```php run(PHP_BINARY . ' startofwork.php'); $task ->weeklyOn(2,'09:00'); // ... ``` -------------------------------- ### Install Crunz using Composer Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use Composer to add Crunz to your project dependencies. ```bash composer require crunzphp/crunz ``` -------------------------------- ### Schedule Task to Run on Specific Day and Time Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Combine weekday methods with `at()` for precise scheduling. This example schedules a task for Mondays at 13:30. ```php run(PHP_BINARY . ' startofwork.php'); $task ->mondays() ->at('13:30'); // ... ``` -------------------------------- ### Set Individual Cron Fields with Mixed Arguments Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Configure individual cron fields using a mix of string ranges, single values, and arrays. This example sets minutes, hours, months, and days of the week. ```php run(PHP_BINARY . ' email.php'); $task ->minute('30') ->hour('13') ->month([1,2]) ->dayofWeek('Mon', 'Fri', 'Sat'); // ... ``` -------------------------------- ### List Defined Crunz Tasks (JSON Format) Source: https://github.com/crunzphp/crunz/blob/3.10/README.md To get a JSON output of the defined tasks, use the schedule:list command with the --format json option. ```bash vendor/bin/crunz schedule:list --format json ``` -------------------------------- ### Define Task Lifetime with Between Dates Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use the `between()` method as an alternative to `from()` and `to()` to define the start and end of a task's active period. Can specify date and time, or just time for daily ranges. ```php run(PHP_BINARY . ' email.php'); $task ->everyFiveMinutes() ->between('12:30 2016-03-04', '04:55 2016-03-10'); // ``` ```php run(PHP_BINARY . ' email.php'); $task ->everyFiveMinutes() ->between('12:30', '04:55'); // ``` -------------------------------- ### Create a Simple Task with Description Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Define a task that creates a backup and includes a descriptive string. Remember to return the Schedule object. ```php run('cp project project-bk'); $task ->daily() ->description('Create a backup of the project directory.'); // ... // IMPORTANT: You must return the schedule object return $schedule; ``` -------------------------------- ### Define a Daily Backup Task Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Create a task file to schedule a daily backup command. Ensure the Schedule object is returned at the end of the file. ```php run('cp project project-bk'); $task->daily(); return $schedule; ``` -------------------------------- ### Schedule Monthly Task on First Day Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use the `monthly()` method to schedule a task that runs once a month on the first day of the month. ```php run(PHP_BINARY . ' email.php'); $task->monthly(); // ... ``` -------------------------------- ### Add Pre-Process and Post-Process Hooks to Tasks Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use the before() and after() methods on Event and Schedule objects to define callbacks that run before and after tasks or all events. Chaining these methods allows for multiple hooks. ```php run(PHP_BINARY . ' email.php'); $task ->everyFiveMinutes() ->before(function() { // Do something before the task runs }) ->before(function() { // Do something else }) ->after(function() { // After the task is run }); $schedule ->before(function () { // Do something before all events }) ->after(function () { // Do something after all events are finished }) ->before(function () { // Do something before all events }); ``` -------------------------------- ### Publish Crunz Configuration Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Execute this command to generate a copy of the default Crunz configuration file in your project's root directory. ```bash /project/vendor/bin/crunz publish:config ``` -------------------------------- ### Schedule Task to Run on Mondays Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use weekday methods like `mondays()` as constraints. Without further time specification, this runs every minute on Mondays. ```php run(PHP_BINARY . ' startofwork.php'); $task->mondays(); ``` -------------------------------- ### Schedule Task at Specific Time Daily Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use the `at()` method in conjunction with a frequency method (like `daily()`) to specify the exact time of day the task should run. ```php run(PHP_BINARY . ' email.php'); $task ->daily() ->at('13:30'); // ... ``` -------------------------------- ### Schedule Daily Task at Midnight Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use the `daily()` method to schedule a task that runs once a day at the beginning of the daily period (midnight). ```php run(PHP_BINARY . ' backup.php'); $task->daily(); // ... ``` -------------------------------- ### Force Run All Crunz Tasks Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use the --force option with the schedule:run command to execute all tasks immediately, regardless of their scheduled time. Useful during development. ```bash vendor/bin/crunz schedule:run --force ``` -------------------------------- ### Schedule Task with Conditional Execution Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use the `when()` method to define a callback that must return `TRUE` for the task to run. Useful for checking resources before executing a task. ```php run(PHP_BINARY . ' email.php'); $task ->everyFiveMinutes() ->between('12:30 2016-03-04', '04:55 2016-03-10') ->when(function() { if ((bool) (time() % 2)) { return true; } return false; }); ``` -------------------------------- ### Schedule Task Using Classic Crontab Syntax Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Schedule tasks using a direct cron expression string, similar to a crontab file. ```php run(PHP_BINARY . ' email.php'); $task->cron('30 12 * 5-6,9 Mon,Fri'); ``` -------------------------------- ### Schedule Task Hourly at Specific Minute Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use `hourlyAt()` to schedule a task to run every hour, but only at a specific minute of that hour. `hourlyOn()` can be used as an alternative. ```php run(PHP_BINARY . ' feedmecookie.php'); $task ->hourlyAt('15'); // ... ``` -------------------------------- ### Avoid setProcess Method Source: https://github.com/crunzphp/crunz/blob/3.10/UPGRADE.md The `\\\Crunz\\\Event::setProcess` method is deprecated and will be removed in v2.0. Avoid using it to prevent exceptions. ```php run('./deploy.sh'); $task ->in('/home') ->weekly() ->sundays() ->at('12:30') ->appendOutputTo('/var/log/backup.log'); // ... return $schedule; ``` -------------------------------- ### Schedule Task Weekly on Specific Day and Time Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use `weeklyOn()` to schedule a task to run on a specific day of the week (Sunday is 0) and at a specific time. ```php run(PHP_BINARY . ' startofwork.php'); $task ->weeklyOn(1,'13:30'); // ... ``` -------------------------------- ### List Defined Crunz Tasks (Text Format) Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use the schedule:list command to display defined tasks in a tabular text format. This helps in identifying tasks and their details. ```bash vendor/bin/crunz schedule:list ``` -------------------------------- ### Task File Return Type Source: https://github.com/crunzphp/crunz/blob/3.10/UPGRADE.md Since v1.12, task files must return an instance of `\Crunz\Schedule`. Ensure your task files conclude with `return $scheduler;`. ```php run('php -v') ->description('PHP version') ->everyMinute(); // Crunz\Schedule instance returned return $scheduler; ``` -------------------------------- ### Define Task Lifetime with From and To Dates Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use `from()` and `to()` methods to specify a date range during which a task should be active. If only time is provided, it applies daily. ```php run(PHP_BINARY . ' email.php'); $task ->everyFiveMinutes() ->from('12:30 2016-03-04') ->to('04:55 2016-03-10'); // ``` -------------------------------- ### Implement Custom Logger Factory Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Create a class that implements LoggerFactoryInterface to use a custom logger. Configure the logger factory in your crunz.yml file. ```php run(PHP_BINARY . ' email.php'); $task ->everyFiveMinutes() ->appendOutputTo('/var/log/crunz/emails.log'); // ``` -------------------------------- ### Schedule Task on Specific Date and Time Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use the `on()` method to schedule a task for a specific date and time. Accepts any format parsable by PHP's `strtotime` function. ```php run(PHP_BINARY . ' email.php'); $task->on('13:30 2016-03-01'); // ... ``` -------------------------------- ### Run Crunz Tasks Every Minute Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Set up a crontab entry to run the Crunz event runner every minute. This command delegates task execution to Crunz. ```bash * * * * * cd /project && vendor/bin/crunz schedule:run ``` -------------------------------- ### Configure Automatic Output Logging Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Enable automatic logging of task output to a file by setting `log_output` to `true` and specifying `output_log_file` in the Crunz configuration. ```yaml # Configuration settings ## ... log_output: true output_log_file: /var/log/crunz.log ## ... ``` -------------------------------- ### Pass String Parameters to Schedule::run Source: https://github.com/crunzphp/crunz/blob/3.10/UPGRADE.md In v3.3, ensure all parameters passed to `Crunz\Schedule::run` are strings. Convert numeric or boolean values to their string representations. ```php run('php', ['-v' => true, 2]); ``` ```php run('php', ['-v' => '1', '2']); ``` -------------------------------- ### Set Individual Cron Fields with Arrays Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Configure individual cron fields like minute, hour, day of month, and month using arrays of values. ```php run(PHP_BINARY . ' email.php'); $task ->minute(['1-30', 45, 55]) ->hour('1-5', 7, 8) ->dayOfMonth(12, 15) ->month(1); ``` -------------------------------- ### Generate a New Crunz Task Skeleton Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Use the make:task command to generate a skeleton file for a new task. You can specify the task name, script, frequency, and constraints. ```bash vendor/bin/crunz make:task exampleOne --run scripts.php --in /var/www --frequency everyHour --constraint mondays ``` -------------------------------- ### Run Task Using Closure in Crunz Source: https://github.com/crunzphp/crunz/blob/3.10/README.md Schedule a task to run a closure instead of a command. The closure can capture variables from its scope using `use`. ```php run(function() use ($x) { // Do some cool stuff in here }); $task ->everyMinute() ->description('Copying the project directory'); return $schedule; ```