babymarkt/cron-bundle

A simple Symfony cron bundle to edit the system crontab.

Installs: 770

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 3

Forks: 1

Open Issues: 2

Type:symfony-bundle

v2.0.2 2023-02-01 10:45 UTC

README

A small bundle to manage cron entries in system crontab.

Build 2.x codecov Packagist Version License PHP from Packagist PHP from Packagist

Installation

You need to require this library through composer:

composer require babymarkt/cron-bundle

If you are using Symfony Flex, the following will happen automatically. Otherwise, you have to enable the bundle on the bundles.php manually:

// config/bundles.php
return [
    // ...
    Babymarkt\Symfony\CronBundle\BabymarktCronBundle::class => ['all' => true],
];

Configuration

Let's start with a minimal setup to run a job every minute:

babymarkt_cron:
  cronjobs:
    my_job: 'my:symfony:command'

After syncing the cronjobs with the command bin/console babymarkt-cron:sync --env=prod, following entry are created in the crontab:

###> /your/project-dir:prod ###
# job 'my_job' (no description)
* * * * * cd /your/project-dir; php bin/console --env=prod my:symfony:command 2>&1 1>>/dev/null
###< /your/project-dir:prod ###

To learn more about CRON see CRON expression on Wikipedia.

Examples

A job running every day at 3:30 AM

babymarkt_cron:
  cronjobs:
    my_job: 
      command: 'my:symfony:command'
      minutes: 30
      hours: 3

A job running every Tuesday every 10 minutes between 1:00 and 4:00 AM

babymarkt_cron:
  cronjobs:
    my_job: 
      command: 'my:symfony:command'
      minutes: '*/10' # or '0,10,20,30,40,50'
      hours: '1-4'
      weekdays: 3 # Tuesday (SUN-SAT: 0-6)

Full configuration reference

babymarkt_cron:
    options:
        # This ID is used to identify the job block in the crontab. If not defined, 
        # it is automatically generated from the project directory and the environment.
        id: ~
        
        # The script to run the commands.
        script: 'bin/console'
        
        # The working directory. Defaults to %kernel.project_dir%.
        working_dir: ~
        
        # Specifies globally where the output should be written to.
        output:
            file: '/dev/null'
            append: true
        
        # Crontab options
        crontab:
            # Crontab executable.
            bin: 'crontab'
            # Path to store the temporary crontab content. Defaults to the system temp dir. 
            tmpPath: ~
            # The user to execute the crontab.
            user: ~
            # Defines whether sudo is used or not.
            sudo: false
    cronjobs:
        # The name of the job definition
        your-first-job-name:
            
            # Definition of the execution time
            # See https://en.wikipedia.org/wiki/Cron#CRON_expression
            minutes: *
            hours: *
            days: *
            months: *
            weekdays: *
            
            # The Symfony command to execute.
            command: ~ # required
            
            # If TRUE, the command isn't executed.
            disabled: false

            # Overwrites the global output settings.
            output:
                file: ~
                append: ~
            
            # Command arguments and options.    
            arguments:
                - '<<your-first-argument>>'
                - '<<your-second-argument>>'
                #...

Commands

babymarkt-cron:drop

Drops all the whole cronjobs block from crontab not considering the configured cronjobs.

babymarkt-cron:dump

Generates the cron entries which may be installed to crontab and shows it on console.

babymarkt-cron:report

Show some reports about the execution of the configured cronjobs. This features required the DoctrineBundle.

babymarkt-cron:sync

Syncs the configured cronjobs with the crontab. Only the related cron block will be affected.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Baby-Markt/cron-bundle.

License

The bundle is available as open source under the terms of the MIT License.