Git Product home page Git Product logo

cron's Introduction

alt text Cron

Job scheduling for Laravel

Cron can be used for easily performing cron jobs in Laravel. If you want to run jobs from the internet or just from the local computer, Cron can help you. For more information how Cron can simplify your job scheduling, please have a look at the raison d'être.



Raison d’être

Simplicity

The aim is to create a simple way to define cron jobs with Laravel. Creating cron jobs with Cron is easy because this tool provides you with a lot of events that you can use to manage all your jobs. For creating a job you only need a job name, a cron expression and a function which will be called as soon as the time has come. Of course PHP is only running if you call it, so you need something in addition which starts Cron.

Accessibility

The Cron starting call can be executed from the same machine where your Laravel is located (for example with crontab) or from everywhere on the internet (for example from a web cron service) - it is just a command execution or route call.

Centralization

The Cron management is centralized at a single point in your application. You define all jobs in PHP and don't have to use other tools. Deactivating or removing a Cron job is only a PHP function call away.

Platform independency

Laravel is a great way to build small and big web applications. But not every application platform runs on a server which allows unix shell access. For these applications Cron provides the possibility to use an external web cron service to start Cron. If you have shell access - great, you can make use of Cron's command to start the job management.

Monitoring

If something went wrong with your jobs, Cron will inform you. Next to the logging to Monolog, to the Laravel logging facility and to the database, you can add an event listener to get information about error jobs or successfully executed jobs. After execution you will receive a detailed report about the Cron run. With the power of PHP and events you can send a mail, a notification or anything else if anything happens. Cron is talkative like your grandma.

My personal comfort zone

At last, Cron is my personal way to manage job scheduling. I am a web application developer, not an infrastructure guy. I like to handle things in PHP and not in the shell. I want to deploy my application to another server without worrying if I have access to crontab or other Linux tools. I really like Laravels event functionality but don't like Laravel commands. Cron management should be easy and powerful at the same time. And finally, I love to handle things at a single place in my application without using the shell or write a PHP file for each job. Cron is the try to manage cron jobs without headaches.


Installation

Laravel 5

  1. Add "liebig/cron": "dev-main" to your /path/to/laravel/composer.json file at the "require": section (Find more about composer at http://getcomposer.org/)
  2. Run the composer update liebig/cron --no-dev command in your shell from your /path/to/laravel/ directory
  3. If you're on Laravel 5.4 or earlier, add 'Liebig\Cron\Laravel5ServiceProvider' to your 'providers' array in the /path/to/laravel/config/app.php file
  4. Migrate the database with running the command php artisan migrate --path=vendor/liebig/cron/src/migrations
  5. Publish the configuration file with running the command php artisan vendor:publish - now you find the Cron configuration file at /path/to/laravel/config/liebigCron.php and this file won't be overwritten at any update
  6. Now you can use Cron everywhere for free

Laravel 4

  1. Add "liebig/cron": "dev-main" to your /path/to/laravel/composer.json file at the "require": section (Find more about composer at http://getcomposer.org/)
  2. Run the composer update liebig/cron --no-dev command in your shell from your /path/to/laravel/ directory
  3. Add 'Liebig\Cron\CronServiceProvider' to your 'providers' array in the /path/to/laravel/app/config/app.php file
  4. Migrate the database with running the command php artisan migrate --package="liebig/cron"
  5. Publish the configuration file with running the command php artisan config:publish liebig/cron - now you find the Cron configuration file at /path/to/laravel/app/config/packages/liebig/cron and this file won't be overwritten at any update
  6. Now you can use Cron everywhere for free

Configuration

Cron is designed to work out of the box without the need of configuration. To enable this a few default values are set. To change Cron's default settings there are two possibilities.

Set methods

You can use the Cron set methods (e.g. setDatabaseLogging, setRunInterval) to change Cron's behaviour. This changes are temporary and the set methods have to be called every time.

Config file

The behaviour values will be loaded from a config file. You can change this values easily by editing in Laravel 5 the /path/to/laravel/app/config/liebigCron.php file and in Laravel 4 the /path/to/laravel/app/config/packages/liebig/cron/config.php file. This is the more permanent way. If you only want to change settings for one run with conditions, we recommend to use the setter methods.

NOTE: All values set via method will overwrite the values loaded from config file.


Example

Cron

If you use Cron's integrated route or command, you only need to listen for the cron.collectJobs event. The best place to do this is in Laravel 5 the /path/to/laravel5/app/Providers/AppServiceProvider.php file at the boot method and in Laravel 4 the /path/to/laravel/app/start/global.php file.

Laravel 5 - AppServiceProvider.php

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {

    //...

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot() {
        // Please note the different namespace
        // and please add a \ in front of your classes in the global namespace
        \Event::listen('cron.collectJobs', function() {

            \Cron::add('example1', '* * * * *', function() {
                // Do some crazy things unsuccessfully every minute
                return 'No';
            });

            \Cron::add('example2', '*/2 * * * *', function() {
                // Do some crazy things successfully every two minute
                return null;
            });

            \Cron::add('disabled job', '0 * * * *', function() {
                // Do some crazy things successfully every hour
            }, false);
        });
    }
}

Laravel 4 - global.php

Event::listen('cron.collectJobs', function() {
    Cron::add('example1', '* * * * *', function() {
                    // Do some crazy things unsuccessfully every minute
                    return 'No';
                });

    Cron::add('example2', '*/2 * * * *', function() {
        // Do some crazy things successfully every two minute
        return null;
    });

    Cron::add('disabled job', '0 * * * *', function() {
        // Do some crazy things successfully every hour
    }, false);
});

Inside the anonymous function you can use all the Laravel and Cron functions. In the next step you have to configure the route or command which will start Cron.

Using Cron's integrated route

If you don't have shell access to your server, you can easily use an online cronjob service (Google knows some good provider). This provider will run Cron's route in a defined interval. The Cron route has to be protected because if someone else than the service provider invokes it, our jobs will be executed too often. For that reason we need a security key in addition to the route path. This key can be generated with the php artisan cron:keygen command call and has to be set in the Cron config file at the key cronKey.

    // Cron application key for securing the integrated Cron run route - if the value is empty, the route is disabled
    'cronKey' => '1PBgabAXdoLTy3JDyi0xRpTR2qNrkkQy'

Now you have to configure the address and run interval at your online cronjob service provider. The address for the integrated Cron route is always http://yourdomain.com/cron.php?key=securitykey. For the above example this address could be http://exampledomain.com/cron.php?key=1PBgabAXdoLTy3JDyi0xRpTR2qNrkkQy and the run interval has to be every minute (due to the job with the name "example1"). Now the jobs were added, the route key was generated and the service provider was configured.

Using Cron's integrated command

If your hosting provider grants you shell access or you can manage cron jobs with a control panel software (e.g. cPanel or Plesk), the best way to run Cron is to use the integrated artisan cron:run command. For the above example the crontab or control panel software command could be * * * * * /usr/bin/php /var/www/laravel/artisan cron:run.

NOTE: If you want to use Cron's in time check, which will test if the time between two Cron run method calls are correct, please configure the key runInterval. In our example we call the route every minute so the value should be 1.


API

Add a cron job

Adding a cron job to Cron is very easy by using the static add function. As parameter the name of the cron job, the cron expression and an anonymous function is needed. The boolean isEnabled is optional and can enable or disable this cron job from execution (default is enabled).

public static function add($name, $expression, $function, $isEnabled = true) {

The name is needed for identifying a cron job if an error appears and for logging.

The expression is a string of five or optional six subexpressions that describe details of the schedule. The syntax is based on the Linux cron daemon definition.

    *    *    *    *    *    *
    -    -    -    -    -    -
    |    |    |    |    |    |
    |    |    |    |    |    + year [optional]
    |    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
    |    |    |    +---------- month (1 - 12)
    |    |    +--------------- day of month (1 - 31)
    |    +-------------------- hour (0 - 23)
    +------------------------- min (0 - 59)

The given anonymous function will be invoked if the expression details match with the current timestamp. This function should return null in the case of success or anything else if there was an error while executing this job. By default, the error case will be logged to the database and to a Monolog logger object (if logger is enabled).

The isEnabled boolean parameter makes it possible to deactivate a job from execution without removing it completely. Later the job execution can be enabled very easily by giving a true boolean to the method. This parameter is optional and the default value is enabled.

Example

\Cron::add('example1', '* * * * *', function() {
                    // Do some crazy things successfully every minute
                    return null;
                });
\Cron::add('example2', '*/2 * * * *', function() {
                    // Oh no, this job has errors and runs every two minutes
                    return false;
                }, true);

Remove a cron job

To remove a set cron job on runtime use the remove method with the cron job name as string parameter.

public static function remove($name) {

Example

\Cron::add('example1', '* * * * *', function() {
                    // Do some crazy things successfully every minute
                    return null;
                });
\Cron::remove('example1');

Enable or disable a cron job

After adding an enabled or disabled cron job ($isEnabled boolean parameter of the add method call) you can disable or enable a cron job by name. For this use the setEnableJob or setDisableJob function.

public static function setEnableJob($jobname, $enable = true) {
public static function setDisableJob($jobname) {

Example

\Cron::add('example1', '* * * * *', function() {
                    // Do some crazy things successfully every minute
                    return null;
                });
\Cron::setDisableJob('example1');
// No jobs will be called
$report = \Cron::run();
\Cron::setEnableJob('example1');
// One job will be called
$report = \Cron::run();

Getter

To receive the enable status boolean of a job, use the static isJobEnabled($jobname) method.


Run the cron jobs

Running the cron jobs is as easy as adding them. Just call the static run method and wait until each added cron job expression is checked. As soon as the time of the expression has come, the corresponding cron job will be invoked. That is the Cron magic. The run method returns a detailed report. By default Cron reckons that you call this method every minute (* * * * *) and by default the report (with their cron jobs errors) will be logged to database. You can change this interval using the setRunInterval function.

public static function run() {

Example

$report = \Cron::run();

NOTE: The run method call has to be the last function call after adding jobs, setting the interval, deactivating database logging and the other function calls.


Set the run interval

The run interval is the time between two cron job route calls. Some cron service provider only supports calls every 15 or even 30 minutes. In this case you have to set this value to 15 or 30. This value is only important to determine if the current run call is in time. If you have disabled database logging in general, you don't have to care about this.

public static function setRunInterval($minutes) {

NOTE: If the route call interval is not every minute you have to adjust your cron job expressions to fit to this interval.

Example

// Set the run intervall to 15 minutes
\Cron::setRunInterval(15);
// Or set the run intervall to 30 minutes
\Cron::setRunInterval(30);

Getter

To recieve the current set run interval use the static getRunInterval() method.


Enable or disable Laravel logging

The Laravel logging facilities provide a layer on top of Monolog. By default, Laravel is configured to create daily log files for your application, and these files are stored in app/storage/logs. Cron will use Laravel logging facilities by default. You can disable this by setting the laravelLogging value to false in the config file or by calling the setLaravelLogging function at runtime.

public static function setLaravelLogging($bool) {

NOTE: You can add a custom Monolog logger to Cron and enable Laravel logging. In this case all messages will be logged to Laravel and to your custom Monolog logger object.

Example

// Laravel logging is enabled by default
\Cron::run();
// Disable Laravel logging
\Cron::setLaravelLogging(false);
// Laravel logging is disabled
\Cron::run();

Getter

To recieve the enabled or disabled boolean value use the static isLaravelLogging() method.


Set a Monolog logger

If you want to add a custom Monolog logger object to Cron use the static setLogger method.

public static function setLogger(\Monolog\Logger $logger = null) {

NOTE: If you want to remove the logger, just call the setLogger method without parameters.

Example

\Cron::setLogger(new \Monolog\Logger('cronLogger'));
// And remove the logger again
\Cron::setLogger();

Getter

To recieve the set logger object use the static getLogger() method. If no logger object is set, null will be returned.


Disable database logging

By default database logging is enabled and after each cron run a manager object and job objects will be saved to database. We strongly recommend to keep the database logging activated because only with this option Cron can check if the current run is in time. It could make sense in some cases to deactivate the database logging with the setDatabaseLogging method.

public static function setDatabaseLogging($bool) {

Example

\Cron::setDatabaseLogging(false);

Getter

To receive the current boolean value of the logging to database variable, just use the static isDatabaseLogging() function.


Log only error jobs to database

By default Cron will log all jobs to database. Maybe sometimes you want to log only error jobs (which not return null) to database by using the static setLogOnlyErrorJobsToDatabase function.

public static function setLogOnlyErrorJobsToDatabase($bool) {

Example

// Log only error jobs to database
\Cron::setLogOnlyErrorJobsToDatabase(true);

Getter

To receive the current boolean value of the error job logging, use the static isLogOnlyErrorJobsToDatabase() function.


Delete old database entries

Cron can delete old database entries for you. During each run method call, Cron checks if there are old manager and job entries in the database and if the reference value is reached, the entries will be deleted. You can change the reference value by calling the setDeleteDatabaseEntriesAfter function. The default value is 240 hours (10 days). To disable the deletion of old entries just set the reference value to 0.

public static function setDeleteDatabaseEntriesAfter($hours) {

Example

// Set the delete database entries reference value to 10 days (24 hours x 10 days)
\Cron::setDeleteDatabaseEntriesAfter(240);

Getter

To receive the current reference value just use the static getDeleteDatabaseEntriesAfter function.


Prevent overlapping

Cron can prevent overlapping. If this is enabled, only one Cron instance can run at the same time. For example if some jobs need 5 minutes for execution but the Cron route will be called every minute, without preventing overlapping two Cron instances will execute jobs at the same time. When running a job twice at the same time, side effects can occur. Cron can avoid such overlaps by using simple locking techniques.

public static function setEnablePreventOverlapping() {

Example

// The configuration could be set via config file with the key 'preventOverlapping' or via method
\Cron::setEnablePreventOverlapping();
// Now the Cron run will only run once at the same time

\Cron::setDisablePreventOverlapping();
// Prevent overlapping is disabled and many Cron run executions are possible at the same time

Getter

To receive the current boolean value just use the static isPreventOverlapping function.

NOTE: To use the overlapping function, Cron needs writing access to the Laravel storage directory. On some Windows machines the lock file cannot be deleted. If you see a delete error message in your log, please disable this feature.


Events

Cron supports Laravel events and provides many information about the run status and the job status. With this you can react to errors. Cron supports the following events.

  • cron.collectJobs - fired before run method call to add jobs and to configure Cron. This event is only fired if you use Cron's integrated route or command.
  • cron.beforeRun - fired before run method call to inform that Cron is about to start. Parameter: $runDate.
  • cron.jobError - fired after a job was exectued and this job returned an error (return value is not equal null). Parameter: $name, $return, $runtime, $rundate.
  • cron.jobSuccess - fired after a job was executed and this job did not return an error (return value is equal null). Parameter: $name, $runtime, $rundate.
  • cron.afterRun - fired after the Cron run was finished. Parameter: $rundate, $inTime, $runtime, $errors - number of error jobs, $crons - array of all exectued jobs (with the keys $name, $return, $runtime), $lastRun - array with information of the last Cron run (with the keys $rundate and $runtime). The $lastRun parameter is an empty array, if Cron is called the first time or if database logging is disabled and therefore the $inTime parameter is equals -1.
  • cron.locked - fired if lock file was found. Parameter: $lockfile.

To subscribe to an event, use Laravels Event facility. The best place for this is the /path/to/laravel/app/start/global.php file.

\Event::listen('cron.jobError', function($name, $return, $runtime, $rundate){
    \Log::error('Job with the name ' . $name . ' returned an error.');
});

Commands

Cron brings you the following Laravel commands.

  • cron:run - fires the cron.collectJobs event and starts Cron.
  • cron:list - fires the cron.collectJobs event and lists all registered Cron jobs.
  • cron:keygen - generates a security token with 32 characters.

Reset Cron

To reset the cron management, call the static reset method. It will reset all variables to the default values.

public static function reset() {

Example

\Cron::add('example1', '* * * * *', function() {
                    // Do some crazy things successfully every minute
                    return null;
                });
\Cron::setLogger(new \Monolog\Logger('cronLogger'));
\Cron::reset();
// \Cron::remove('example1') === false
// \Cron::getLogger() === NULL

Frequently Asked Questions

Do I really need crontab or an online cronjob service

Yes, you do. In comparison to a Java application server for example, PHP only runs if it is executed. If crontab or an online cronjob service provider calls PHP and starts the application, Cron can execute the jobs and will start the work. If PHP is not started, the application sleeps and nothing happens.

What is the best interval to call the route or command?

The best interval depends on your jobs. If one job should be executed every minute and another every five minutes, the route or command has to be called every minute. In general you have to find the greatest common divisor of your jobs. Please don't forget to change the runInterval config value if the route or command is not called every minute (default value) and if you want to use Cron's in time check.

Cron is not running properly and returns runtime and inTime with value -1

By default Cron prevents overlapping. This means that only one Cron instance will run at the same time. If another instance is called, Cron will not run and will return the runtime and inTime parameter with the value -1. On some Windows machines the deletion of the lock file fails and you have to disable this feature. Please have a look at the prevent overlapping section.


Changelog

2019/05/21 - 1.3

  • Bugfixing

2017/09/13 - 1.2

  • Adding support for Laravel 5.5
  • Adding support for Laravel 5.4

2016/11/15 - 1.1.2

  • Adding support for Symfonys 3 Table class in Laravel 5.2

2016/06/07 - 1.1.1

  • Fixing bug with Laravel 5.2

2015/03/02 - 1.1.0

  • Adding Laravel 5 support
  • Adding index to 'cron_job' table
  • Removing Eloquent class aliases
  • Changing 'cron_manager_id' column of 'cron_job' table to unsigned
  • Fixing 'setDisableInTimeCheck' method

2015/02/02 - 1.0.2

  • Adding cron.locked event
  • Marking Cron as stable
  • Changing $function parameter type to "callable" to fix IDE type hints

2014/10/13 - 1.0.1

  • Adding try-catch-finally block to the run method to always remove the lock file
  • Adding $lastRun parameter to the cron.afterRun event
  • Adding Laravel 5 composer support
  • Removing return-string truncating after 500 characters
  • Fixing cron.afterRun event

2014/06/12 - 1.0.0

  • Adding Laravel route with security token
  • Adding Artisan command for generating security token
  • Adding Artisan command for running Cron easily with e.g. crontab
  • Adding Artisan command for list all jobs, added via event
  • Adding events
  • Adding overlapping protection
  • Changing default value for config key 'logOnlyErrorJobsToDatabase' to false
  • Fixing PHP doc markup
  • Generating API
  • Refurbishing this README file
  • Minor bug fixes

2014/02/11 - v0.9.5

  • Bug fixing release
  • Fixing bug with PSR0 Autoloading
  • Fixing time bug - if a job took more than one minute for execution the following jobs were not handled

2013/11/12 - v0.9.4

  • Adding Laravel logging facilities - by default Cron will log to Laravel now
  • Adding Exceptions - Cron will throw InvalidArgumentExceptions and UnexpectedValueExceptions now
  • Minor bug fixes

2013/11/01 - v0.9.3

  • Adding facade for Cron - you can use Cron instead of \Liebig\Cron\Cron now
  • Adding facade test cases

License

The MIT License (MIT)

Copyright (c) 2013 - 2016 Marc Liebig

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Icon Copyright (c) Timothy Miller (http://www.iconfinder.com/icondetails/171279/48/alarm_bell_clock_time_icon) under Creative Commons (Attribution-Share Alike 3.0 Unported) License - Thank you for this awesome icon you own

cron's People

Contributors

ameenross avatar dcelasun avatar harryxu avatar hightowersu avatar levacic avatar liebig avatar marinalans avatar mrcool271 avatar ngyikp avatar problematik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cron's Issues

Checking for missed cron job?

Hi,
Is there a way using cron.afterRun and $inTime to check for a job not being run when it was supposed to? For example, I have the a job that gets added in a cron.collectJobs listener that is supposed to run everyday at a certain time and it just so happens that server reboot was required or maybe Laravel was down at that time. Would it be possible to detect that the cron job was not executed in the cron.afterRun so that I can execute the command that was supposed to be previously executed? If so, could you show a code example?
Thanks in advance,
Troy

Standard interface to run cron jobs, precluding the need for routes

It isn't clear to me where to put my cron definitions. I am writing a package that requires cron to do some pulling of data from a remote source. I defined a cron rule in my service provider's boot method, as that seems like the right place for it.

Apparently through, the facade for the "cron" class is added after my boot method ran, which means I can only use it with the fully qualified class name. I could, however, get it working by editing CronServiceProvider.php to directly add the facade in the register method, like so:

$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Cron', 'Liebig\Cron\Facades\Cron');

Am I doing something wrong, should I put my definition somewhere else? Is the above suggestion flawed for some reason? Or should I just put the facade in my app.config?

Corn job execute only once

Iam using your example code in global.php
Event::listen('cron.collectJobs', function() {

Cron::add('send email', '* * * * *', function() {
    mail('[email protected]', 'My Subject', 'message');
    return null;
});


Cron::add('disabled job', '0 * * * *', function() {
    // Do some crazy things successfully every hour
}, false);

});

And when I do http:root/cron.php?key=$key

only Once cron job will execute and table also get updated once.How to execute this function every minute ?

Provide a secure route that calls the artisan cron:run command

Follow-up of #20

The package should provide a route that calls Artisan::call('cron:run'). This should of course have some form of protection to prevent anyone from accessing the URL inadvertently, potentially causing issues. So my idea is that there should be an automatically generated security token, which will be saved to a configuration file.

A few things to consider (hey, I don't have all the answers):

  • What would the URI path of the route be, so as to not clash with anything existing?
  • How to reconcile with the general idea that routes should be created by the app developer as opposed to packages? Possibly:
    • Use configuration to determine the URL, with default being disabled completely to prevent clashing?
    • Leave the creation of the route to the app developer anyway, but provide example code that includes the security token validation?
  • How to generate configuration values while the app developer runs php artisan config:publish liebig/cron?

How to call method every hour?

I tried that, but it does not work:

Event::listen('cron.collectJobs', function() {
    Cron::add('parseEvents', '0 0-23 * * *', function() {
            Log::info('test');
     });
});

in app/start/global.php

Time to run cron

I have create a cron job run every 2 minutes
Cron::add('collect', '*/2 * * * *', function() {
// code to insert database to table test
});

When I run cmd php artisan cron:run first time, it insert record into table cron_job and cron_manager and not insert record to table test.

I wait more 2 minutes and run cron again but it not insert into table test. I don't know why?

Log file content is:
[2014-12-02 17:27:56] production.ERROR: Cron run with manager id 2 is with 227 seconds between last run too late. [] []
[2014-12-02 17:27:56] production.INFO: The cron run with the manager id 2 was finished without errors. [] []

Getting warning: Lock file found

Hi,
My liebig cron jobs are no longer running and I have not changed any settings on my server. The issue seemingly started to occur shortly after I did a composer update for this 3-4 weeks back. I am currently getting the following logged to the laravel log:

dev.WARNING: Lock file found - Cron is still running and prevent job overlapping is enabled - second Cron run will be terminated.

Any ideas on what is causing this and how I can fix it? Which lock file is being referenced here?

Thanks,
Troy

A bit confuse about how to using.

I read the Readme about 3 times and I have a bit confuse with how to start to cron my job.
So I have question about how to start and is I'm do the right thing ?

OK. I will start at if I want to update my database every 4 AM. and 5 AM.

1. I start at create a crontab -e
0   4   *   *   *  /usr/bin/php /var/www/laravel/artisan cron:run
0   5   *   *   *  /usr/bin/php /var/www/laravel/artisan cron:run
2. I start add a cron job on start/global.php
Event::listen('cron.collectJobs', function() {
    Cron::add('example1', '0 4 * * *', function() {
                    // update my article table
                    // how can I call my controller and method ?
                    $newdata = ArticleController@addnewdata()
                    if ($newdata){
                    // if it update success return "yes"
                    return 'YES';
                    } 
                    return 'NO';
                });

    Cron::add('example2', '0 5 * * *', function() {
        // update my comment
       // if it success return "YES"
        return 'YES';
    });
});

and my "YES" will show on liebig/cron table ?

Thank you verymuch.

Class 'Liebig\Cron\Facades\Cron' not found

Class 'Liebig\Cron\Facades\Cron' not found

Ubuntu 13.04
PHP 5.4.9
Laravel 4.1

Fresh install of Laravel, and isntalled Cron following your install instructions in Readme

Create new tag for v0.9.6

The last release (v0.9.5) is from February and it doesn't include several important bits, like RunCommand.

Can you please tag a new release so those of use who don't want to use dev-master can update?

Also the v in the version is preventing us from using "liebig/cron": "0.9.*" as "liebig/cron": "v0.9.*" is not allowed in composer. So maybe you could start tagging releases without the v?

linux case issue

php artisan migrate --package="Liebig/Cron"

should be

php artisan migrate --package="liebig/cron" 

Usage on windows with integrated command

Hi,

this is not really an issue, but rather some help I need that I couldn't find in the documentation.

I'm missing info on how to use this on windows.
using git bash and typing in the url rout (http: //localhost:800/cron/run/key) my command runs correctly. Now the cron (setup to run automaticlly) is giving me problems.

is it possible to run on windows and what are my next steps? I rather not use the windows task manager to run a batch file.

Note1: from the git bash in the root folder I ran ' * * * * * php artisan cron:run ' but get an error (it reads ./contributing.md from the root folder), so I'm doing something wrong there

Note2: running ' php artisan cron:list ' I get an emtpy list (table)

hi! i have problem

hi
I use liebig/cron

it worked good this morning.
but now not working

--this situation

php artisan cron:list

+----------+------------+-----------+
| Jobname | Expression | Activated |
+----------+------------+-----------+
| example1 | * * * * * | Enabled |
| example3 | * * * * * | Enabled |
| example2 | 28 * * * * | Enabled |
+----------+------------+-----------+

but

php artisan cron:run

+------------+---------+----------+--------+------+
| Run date | In time | Run time | Errors | Jobs |
+------------+---------+----------+--------+------+
| 1409020245 | -1 | -1 | 0 | 0 |
+------------+---------+----------+--------+------+

always keep to occur
help me please

php ide warning

     * @static
     * @param  string $name The name for the cron job - has to be unique
     * @param  string $expression The cron job expression (e.g. for every minute: '* * * * *')
     * @param  function $function The anonymous function which will be executed
     * @param  bool $isEnabled optional If the cron job should be enabled or disabled - the standard configuration is enabled
     * @throws InvalidArgumentException if one of the parameters has the wrong data type, is incorrect or is not set
     */
    public static function add($name, $expression, $function, $isEnabled = true) {

please change the function to Closure, just like follow

     * @static
     * @param  string $name The name for the cron job - has to be unique
     * @param  string $expression The cron job expression (e.g. for every minute: '* * * * *')
     * @param  Closure $function The anonymous function which will be executed
     * @param  bool $isEnabled optional If the cron job should be enabled or disabled - the standard configuration is enabled
     * @throws InvalidArgumentException if one of the parameters has the wrong data type, is incorrect or is not set
     */
    public static function add($name, $expression, $function, $isEnabled = true) {

'Database has gone away' crashes cron and leaves cron.lock file

Hi, We are having some issues with database temporarily gone away which causes the cron job to crash and leaves the cron.lock file there forever. We have disabled the old logging rotation script that causes the database to be offline, but in case if the database might go away temporarily because of other reasons, what's the best way to prevent the cron.lock file from being left forever?

Here is the stack trace if that's relevant:

Sep 26 03:05:05 LT-WEB8 Laravel:  [2014-09-26 03:05:04] NULL.ERROR: exception 'ErrorException' with message 'PDO::__construct(): MySQL server has gone away' in D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:47 
Sep 26 03:05:05 LT-WEB8 Laravel:  Stack trace: 
Sep 26 03:05:05 LT-WEB8 Laravel:  #0 [internal function]: Illuminate\Exception\Handler->handleError(2, 'PDO::__construc...', 'D:\web\emcl\pas...', 47, Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #1 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(47): PDO->__construct('mysql:host=sql....', '', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #2 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Connectors\MySqlConnector.php(20): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=sql....', Array, Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #3 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(59): Illuminate\Database\Connectors\MySqlConnector->connect(Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #4 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(47): Illuminate\Database\Connectors\ConnectionFactory->createSingleConnection(Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #5 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(127): Illuminate\Database\Connectors\ConnectionFactory->make(Array, 'emcl') 
Sep 26 03:05:05 LT-WEB8 Laravel:  #6 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(63): Illuminate\Database\DatabaseManager->makeConnection('emcl') 
Sep 26 03:05:05 LT-WEB8 Laravel:  #7 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2810): Illuminate\Database\DatabaseManager->connection(NULL) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #8 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2776): Illuminate\Database\Eloquent\Model::resolveConnection(NULL) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #9 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1791): Illuminate\Database\Eloquent\Model->getConnection() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #10 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1714): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #11 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2999): Illuminate\Database\Eloquent\Model->newQuery() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #12 [internal function]: Illuminate\Database\Eloquent\Model->__call('orderBy', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #13 [internal function]: Liebig\Cron\Models\Manager->orderBy('rundate', 'DESC') 
Sep 26 03:05:05 LT-WEB8 Laravel:  #14 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3015): call_user_func_array(Array, Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #15 D:\web\emcl\passport\builds\api\291\vendor\liebig\cron\src\Liebig\Cron\Cron.php(188): Illuminate\Database\Eloquent\Model::__callStatic('orderBy', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #16 D:\web\emcl\passport\builds\api\291\vendor\liebig\cron\src\Liebig\Cron\Cron.php(188): Liebig\Cron\Models\Manager::orderBy('rundate', 'DESC') 
Sep 26 03:05:05 LT-WEB8 Laravel:  #17 D:\web\emcl\passport\builds\api\291\vendor\liebig\cron\src\Liebig\Cron\RunCommand.php(40): Liebig\Cron\Cron::run() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #18 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Console\Command.php(108): Liebig\Cron\RunCommand->fire() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #19 D:\web\emcl\passport\builds\api\291\vendor\symfony\console\Symfony\Component\Console\Command\Command.php(241): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\NullOutput)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #20 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Console\Command.php(96): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\NullOutput)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #21 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Console\Application.php(96): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\NullOutput)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #22 [internal function]: Illuminate\Console\Application->call('cron:run', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #23 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Foundation\Artisan.php(57): call_user_func_array(Array, Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #24 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(211): Illuminate\Foundation\Artisan->__call('call', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #25 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(211): Illuminate\Foundation\Artisan->call('cron:run', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #26 D:\web\emcl\passport\builds\api\291\vendor\liebig\cron\src\Liebig\Cron\CronServiceProvider.php(46): Illuminate\Support\Facades\Facade::__callStatic('call', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #27 D:\web\emcl\passport\builds\api\291\vendor\liebig\cron\src\Liebig\Cron\CronServiceProvider.php(46): Illuminate\Support\Facades\Artisan::call('cron:run', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #28 [internal function]: Liebig\Cron\CronServiceProvider->Liebig\Cron\{closure}() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #29 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Routing\Route.php(105): call_user_func_array(Object(Closure), Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #30 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1000): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #31 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Routing\Router.php(968): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #32 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(738): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #33 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(708): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #34 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Http\FrameGuard.php(38): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #35 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Session\Middleware.php(72): Illuminate\Http\FrameGuard->handle(Object(Illuminate\Http\Request), 1, true) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #36 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Cookie\Queue.php(47): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #37 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Cookie\Guard.php(51): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #38 D:\web\emcl\passport\builds\api\291\vendor\stack\builder\src\Stack\StackedHttpKernel.php(23): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #39 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(606): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #40 D:\web\emcl\passport\builds\api\291\public\index.php(49): Illuminate\Foundation\Application->run() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #41 {main} 
Sep 26 03:05:05 LT-WEB8 Laravel:  Next exception 'PDOException' with message 'SQLSTATE[HY000] [2006] MySQL server has gone away' in D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:47 
Sep 26 03:05:05 LT-WEB8 Laravel:  Stack trace: 
Sep 26 03:05:05 LT-WEB8 Laravel:  #0 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(0): PDO->__construct() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #1 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Connectors\MySqlConnector.php(20): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=sql....', Array, Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #2 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(59): Illuminate\Database\Connectors\MySqlConnector->connect(Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #3 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(47): Illuminate\Database\Connectors\ConnectionFactory->createSingleConnection(Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #4 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(127): Illuminate\Database\Connectors\ConnectionFactory->make(Array, 'emcl') 
Sep 26 03:05:05 LT-WEB8 Laravel:  #5 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(63): Illuminate\Database\DatabaseManager->makeConnection('emcl') 
Sep 26 03:05:05 LT-WEB8 Laravel:  #6 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2810): Illuminate\Database\DatabaseManager->connection(NULL) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #7 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2776): Illuminate\Database\Eloquent\Model::resolveConnection(NULL) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #8 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1791): Illuminate\Database\Eloquent\Model->getConnection() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #9 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1714): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #10 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2999): Illuminate\Database\Eloquent\Model->newQuery() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #11 [internal function]: Illuminate\Database\Eloquent\Model->__call('orderBy', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #12 [internal function]: Liebig\Cron\Models\Manager->orderBy('rundate', 'DESC') 
Sep 26 03:05:05 LT-WEB8 Laravel:  #13 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3015): call_user_func_array(Array, Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #14 D:\web\emcl\passport\builds\api\291\vendor\liebig\cron\src\Liebig\Cron\Cron.php(188): Illuminate\Database\Eloquent\Model::__callStatic('orderBy', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #15 D:\web\emcl\passport\builds\api\291\vendor\liebig\cron\src\Liebig\Cron\Cron.php(188): Liebig\Cron\Models\Manager::orderBy('rundate', 'DESC') 
Sep 26 03:05:05 LT-WEB8 Laravel:  #16 D:\web\emcl\passport\builds\api\291\vendor\liebig\cron\src\Liebig\Cron\RunCommand.php(40): Liebig\Cron\Cron::run() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #17 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Console\Command.php(108): Liebig\Cron\RunCommand->fire() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #18 D:\web\emcl\passport\builds\api\291\vendor\symfony\console\Symfony\Component\Console\Command\Command.php(241): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\NullOutput)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #19 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Console\Command.php(96): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\NullOutput)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #20 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Console\Application.php(96): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\NullOutput)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #21 [internal function]: Illuminate\Console\Application->call('cron:run', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #22 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Foundation\Artisan.php(57): call_user_func_array(Array, Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #23 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(211): Illuminate\Foundation\Artisan->__call('call', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #24 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(211): Illuminate\Foundation\Artisan->call('cron:run', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #25 D:\web\emcl\passport\builds\api\291\vendor\liebig\cron\src\Liebig\Cron\CronServiceProvider.php(46): Illuminate\Support\Facades\Facade::__callStatic('call', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #26 D:\web\emcl\passport\builds\api\291\vendor\liebig\cron\src\Liebig\Cron\CronServiceProvider.php(46): Illuminate\Support\Facades\Artisan::call('cron:run', Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #27 [internal function]: Liebig\Cron\CronServiceProvider->Liebig\Cron\{closure}() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #28 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Routing\Route.php(105): call_user_func_array(Object(Closure), Array) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #29 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1000): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #30 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Routing\Router.php(968): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #31 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(738): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #32 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(708): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #33 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Http\FrameGuard.php(38): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #34 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Session\Middleware.php(72): Illuminate\Http\FrameGuard->handle(Object(Illuminate\Http\Request), 1, true) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #35 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Cookie\Queue.php(47): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #36 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Cookie\Guard.php(51): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #37 D:\web\emcl\passport\builds\api\291\vendor\stack\builder\src\Stack\StackedHttpKernel.php(23): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #38 D:\web\emcl\passport\builds\api\291\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(606): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request)) 
Sep 26 03:05:05 LT-WEB8 Laravel:  #39 D:\web\emcl\passport\builds\api\291\public\index.php(49): Illuminate\Foundation\Application->run() 
Sep 26 03:05:05 LT-WEB8 Laravel:  #40 {main} [] [] 

Package installation broken for Laravel 5

In step 4 of the installation process, I get an error (below) when trying to run the Artisan migration:

Digging around, I've found a discussion on the Laracasts forums where it's pointed out this could be related to the new structure of Laravel 5.

Anybody has any idea about this?

exception 'BadMethodCallException' with message 'Call to undefined method [package]' in /home/vagrant/myApplication/vendor/laravel/framework/src/Illuminate/Support/ServiceProvider.php:226
Stack trace:
#0 /home/vagrant/myApplication/vendor/liebig/cron/src/Liebig/Cron/CronServiceProvider.php(22): Illuminate\Support\ServiceProvider->__call('package', Array)
#1 /home/vagrant/myApplication/vendor/liebig/cron/src/Liebig/Cron/CronServiceProvider.php(22): Liebig\Cron\CronServiceProvider->package('liebig/cron')
#2 [internal function]: Liebig\Cron\CronServiceProvider->boot()
#3 /home/vagrant/myApplication/vendor/laravel/framework/src/Illuminate/Container/Container.php(523): call_user_func_array(Array, Array)
#4 /home/vagrant/myApplication/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(631): Illuminate\Container\Container->call(Array)
#5 /home/vagrant/myApplication/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(613): Illuminate\Foundation\Application->bootProvider(Object(Liebig\Cron\CronServiceProvider))
#6 [internal function]: Illuminate\Foundation\Application->Illuminate\Foundation\{closure}(Object(Liebig\Cron\CronServiceProvider), 19)
#7 /home/vagrant/myApplication/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(614): array_walk(Array, Object(Closure))
#8 /home/vagrant/myApplication/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php(15): Illuminate\Foundation\Application->boot()
#9 /home/vagrant/myApplication/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(165): Illuminate\Foundation\Bootstrap\BootProviders->bootstrap(Object(Illuminate\Foundation\Application))
#10 /home/vagrant/myApplication/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(192): Illuminate\Foundation\Application->bootstrapWith(Array)
#11 /home/vagrant/myApplication/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(89): Illuminate\Foundation\Console\Kernel->bootstrap()
#12 /home/vagrant/myApplication/artisan(36): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 {main}

Cron not work on midnight

Hello,thanks for your library for Cron in laravel.
i test for every minute for cron
and add crontab for every minute it's work fine.
but i wanna cron for midnight and change * * * * to 0 0 * * for my Cron and crontab and boom not work...
checked cron manager on database and see rundate time is 05:10:25 like that any idea?!
screenshot from 2015-03-12 09 29 43
screenshot from 2015-03-12 09 32 00

Configuration file problem

The configuration file is named Config.php, but Laravel uses config.php when loading package-specific configuration files. This doesn't affect Windows users, but the configuration is completely ignored on Linux, and all settings default to falsey values (because Config::get('cron::setting') returns null, since it can't find the config file).

This also affects publishing, since artisan config:publish just copies the file from the vendor folder as-is, so it will also be copied with the wrong name.

The solution is simple - just lowercase the filename.

I'll send a pull request for this in a couple of minutes.

Cron

I create a test cron every minute
Event::listen('cron.collectJobs', function() {
Cron::add('test', '* * * * *', function() {
Log::info('test');
});
});

When I open my website and it will auto run cron every minute, right?
Must do I do anything more to do that? Ex: run cron by cmd: php artisan cron:run

Controller method not found

Hi Liebig,

I have an issue with cron. Installed according to your description without any problem.

I have basic Laravel application with Auth users and Route controller in context root:

Route::controller('/', 'UsersController');

When I'm trying to run my cron:
http://mydomain.com/cron.php?key=mykey

I'm getting "Controller method not found" exception.

Don't you know where the problem could be? Are Laravel's controllers executed before your
\Route::get('cron.php', function() {
code?

How to execute command in the cron?

I am able to execute the command in Terminal like this:
php artisan doSomething

Is the followings correct to execute the command within the cron?

This is my global.php:

Event::listen('cron.collectJobs', function() {
Cron::add('doSomething', '* * * * *', function() {
// Run every minute
Artisan::call('doSomething');
return null;
});
});

But php artisan cron:run always return -1:
+------------+---------+----------+--------+------+
| Run date | In time | Run time | Errors | Jobs |
+------------+---------+----------+--------+------+
| 1402831170 | -1 | -1 | 0 | 0 |
+------------+---------+----------+--------+------+

Issue logging cron job errors only.

Hi,
I am using crontab to execute the artisan cron:run command every minute and did not want the cron_manager table being logged to but I did want job errors logged to the cron_job table so I thought having my config.php configured as follows would solve that:

'databaseLogging' => true,
'logOnlyErrorJobsToDatabase' => true,

This didn't work as expected because the cron_manager table continued to still get updated every minute. The only way I could get cron to stop logging to the cron_manager table was to change the configuration to:

'databaseLogging' => false,

Which then stopped my result from getting logged to the cron_job table. Is this the way the feature is suppossed to work? I would expect that if logOnlyErrorJobsToDatabase is set to true then the cron_manager and cron_job tables would only get updated when an error occurred.

Thanks in advance,
Troy Carpenter

Could not find driver only in cron

Hello,
i have a controller from which i connect to an sql server and i get some results.
Where i run the function using the web interface, the function runs with no problem.

my controller:

class ProductController extends \BaseController {
public function __construct(Setting $setting, SqlServer $sqlserver, Opencart $opencart)
    {
        $this->setting = $setting;
        $this->sqlserver = $sqlserver;
        $this->connectionOC = 'localOC';
    }
public function addNewProducts() {
        $opencart = $this->connectionOC;
        $t = $this->setting->getLastProductTimestamp(); 

        $products = $this->sqlserver->getSeries();
return $products;
}
}

The cron:

Event::listen('cron.collectJobs', function() {
    Cron::add('AddNewProducts', '* * * * *', function() {
        $pr = App::make('ProductController')->addNewProducts();
    });

});

If run php artisan cron:run and i get in db log "Exception in job AddNewProducts: could not find driver".
I have tested the previous connection eg. $this->setting->getLast..... and it works.
The error comes drom sqlserver connection, it seems that it cannot find the driver which is already set and working all over the other application.

What am i doing wrong?

Thanks
-Konstantinos

Unable to run cron

Hey,

$ * * * * * /usr/bin/php $HOME/Documents/etm/api/artisan cron:run

gives me

zsh: command not found: app

Maybe, zsh is the culprit here?

edit1: same with bash

$ * * * * * /usr/bin/php $HOME/Documents/etm/api/artisan cron:run
No command 'app' found, but there are 17 similar ones
app: command not found

edit2: reading http://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work
edit3: just going to use external service...

not added to cron_jobs?

    Cron::add('testcron', '*/5 * * * *', function() {
        return null;
    }, true);
    Cron::setEnableJob('testcron');
    print_r(Cron::run());

Nothings added to the cron_jobs table, did I do something wrong?

Conflicts with Jeffery Way Generators

Seems to be a weird conflict with generators.
if i uninstall generators it works fine. But i must have my generators!!! :)

php artisan migrate --package="liebig/cron"
PHP Fatal error: Class 'Way\Generators\GeneratorsServiceProvider' not found in /vagrant/www/lms/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 158
PHP Stack trace:
PHP 1. {main}() /vagrant/www/lms/artisan:0
PHP 2. require_once() /vagrant/www/lms/artisan:30
PHP 3. require() /vagrant/www/lms/bootstrap/start.php:60
PHP 4. Illuminate\Foundation\ProviderRepository->load() /vagrant/www/lms/vendor/laravel/framework/src/Illuminate/Foundation/start.php:210
PHP 5. Illuminate\Foundation\ProviderRepository->compileManifest() /vagrant/www/lms/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php:58
PHP 6. Illuminate\Foundation\ProviderRepository->createProvider() /vagrant/www/lms/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php:122
{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"Class 'Way\Generators\GeneratorsServiceProvider' not found","file":"/vagrant/www/lms/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php","line":158}}

Where is the locate of folder src?

Where must I have put folder src and tests?

When I run "composer update liebig/cron --no-dev" return the error

Problem 1

  • The requested package liebig/cron 1.0.0 could not be found.

Protential causes:

Read http://getcomposer.org/doc/articles/troubleshooting.md for further common
roblems.

Please help me. Thanks!

Undefined index: inTime

i try to run cron in command "php artisan cron:run" but show me this error
"[ErrorException]
Undefined index: inTime"

can some one please explain me show can i run cron on my machine, i used windows ?

when i go to cron url path /cron/run/sdasdasdawq2323123 show me this message "Array ( [rundate] => 1400113159 [runtime] => -1 ) " is it error or what ?

Passing variables

Is it possible to pass a variable to Cron::add function?

$variable = 'testing';

Cron::add('example', '* * * * *', function($variable) {
Log::info($variable);
return null;
}

will cron queue work in localhost ?

I am beginner to the laravel 4 and cron queue. I want to use the cron queue to post the posts to facebook page in particular time . Can any one help ..?

Lock File

I have this configuration

1 - cronjob (every 1 hour)
2 - cronjob( everny 10 minutes)

if the 1 cronjob takes 30minutes to run, the second is going to be executed in this period 3 time's or not because is locked ?

linux class not found

Class 'Liebig\Cron\models\Manager' not found

if (self::isDatabaseLogging()) {
// Get the time (in seconds) between this and the last run and save this to $timeBetween
$lastManager = \Liebig\Cron\models\Manager::orderBy('rundate', 'DESC')->take(1)->get();
if (!empty($lastManager[0])) {
$lastRun = new \DateTime($lastManager[0]->rundate);

Problem with laravel while running a job

Hi!

I'm not sure if this could be an issue but I cannot find any information about it, neither on the apache-php nor the laravel forums.

I'm running a job using your bundle and it takes about 5-6 seconds to complete. This job is just a loop of about 20 requests of an URI which returns a simple json response each time is called. Then I parse every response in turn.

Well, the problem is that while this job is running I can't make any concurrent request to any page of my laravel-based site. Actually, I can make the request but
I have to wait until the job has finished to get a response to the concurrent request.

I have tested in different environments (dev, production) and servers (apache, laravel built-in server) and the problem remains the same.

Thank you for any information or direction you can provide.

artisan command?

Is it possible to move the run() into a command instead of using a route? Then just fire that on the cronjob using php artisan command instead of using wget?

I haven't installed this yet to try.

Thanks.

Exception `Impossible CRON expression` raised on php-5.5.15(debian)

Hi @liebig , Here raise an exception after I upgrade my php from 5.4.9 to 5.5.15 today.

I did not change anything of my code, and I found that there raise an exception said Impossible CRON expression in CronExpression.php line at line 321 and cron.lock created in storage path.

for example, all the follow will not work(Impossible CRON expression)

0 1 * * *
0 2 * * *
0 4 * * *
0 5 * * *

but the follow will work!

0 0 * * *
0 3 * * *
0 6 * * *
0 9 * * *
0 12 * * *

so strage issue. need your help!!!

Cron jobs running at UTC time

I'm on Eastern US time, the server is set to ET, but the jobs are running 5 hours out of whack.

It appears the library uses the timestamp which is based off UTC time.

Is there a way to make it respect the timezone of the system it's on?

[Questions] The use for this library

I have a questions for this library, if I can use/get a server for call the request for the url, why I need this cron library? And this library also require a crontab or third party server to call the url. I am a kind of confuse..

running on multiple server instances

I would like to use this package for a site which will run on multiple ec2 instances of amazon web services behind a load balancer. Now I see two options:

  • Integrate the crontab setup within my deploy script to setup the cron on every instance. How do I prevent multiple executions at the same time / overlapping ?
  • Setup an external server to call the route from outside and the loadbalancer chooses an instance on which it will be executed. ( I think this could work but isn't that nice in my opinion )

What do you think? What is the way to go?

How to raise the "Lock file found" event ?

Hi,
I don't understand why the cron.lock is not deleted sometimes without errors, is there a way to raise the "Lock file found" event so I can send me a mail to check what's is wrong ?
Thanks,
Pierre.

Result truncated to 500 bytes when saved to cron_job table

I've noticed that the result is truncated to 500 bytes when saved to cron_job.

I'm using a json_encoded result to indicate successful execution of the tasks, together with additional information about the execution, and often this can be longer than 500 bytes when json encoded; so the result written to the log is invalid json.

As the field is defined as text, and so would permit much longer data volumes to be stored, is it possible to make this a configurable option: either to disable the truncation, and/or to configure the size at which the result is truncated.

I'll happily submit a PR if you're open to the idea

Jobs execute just once.

Hello,

I have a script checking errors on webpages. I'm trying to develop a tool with Laravel to manage 'alerts' (automated calls to this script to maintain a surveillance).
But I'm a bit confused about how to using your lib.

When I add a cron like this :

Route::get('/crontest', function() {
        Cron::add('example1', '* * * * *', function() {
               return 'Example 1 ran';
        });    
        $report = \Liebig\Cron\Cron::run();
        var_dump($report);
});

I get a new line in cron_manager like this :
| 719 | 2014-12-04 14:29:02 | 0.00 |

And a new line in cron_jobs like this :
| 11 | example1 | Example 1 ran | 0.00 | 719 |

But nothing else. As far as I understand, I should have a new line in cron_manager every minutes. But I don't.

Must I use Event::listen('cron.collectJobs') to add crons ? What does it concretely do ?
Can I call Cron::add everywhere in the app ?

Sorry for my mistakes, English isn't my native language.

Thank you in advance.

Some jobs not executed

Hi Liebig,

Some of the jobs I schedule never get executed.

For example, a job with expression '25 11 * * 4' which should execute each Thursday at 11:25 is skipped (cron manager does execute fine). It seems to me that the isDue function is not parsing my expression correctly so it is never matched?

Any help would be greatly appreciated.

Thanks for this awesome package!

No update to cron_job db

I try to run cronjob like this

Event::listen('cron.collectJobs', function() {
    Cron::add('example1', '* * * * *', function() {
            // Do some crazy things unsuccessfully every minute
            return 'No';
        });
});

and I set my crontab like this

* * * * * /usr/bin/php /var/www/mylaravel/artisan cron:run

and wait about 1 min
the cron_manager have a value
1 2014-08-13 21:27:03 -1.00
2 2014-08-13 21:28:03 -1.00
3 2014-08-13 21:29:03 -1.00
4 2014-08-13 21:30:03 -1.00
5 2014-08-13 21:31:03 -1.00
6 2014-08-13 21:32:03 -1.00
7 2014-08-13 21:33:03 -1.00

but the cron_job no value
I expect it has a value "NO" on return column

What I do wrong?

linux class not found

Class 'Liebig\Cron\models\Manager' not found

if (self::isDatabaseLogging()) {
// Get the time (in seconds) between this and the last run and save this to $timeBetween
$lastManager = \Liebig\Cron\models\Manager::orderBy('rundate', 'DESC')->take(1)->get();
if (!empty($lastManager[0])) {
$lastRun = new \DateTime($lastManager[0]->rundate);

problem with repaet job

hi ...
I after add new cron , played it ...
But only once the function is executed ...
i use full example in readme ...
What should I do to make this cron will continue ?

Run Added Tasks Locally, Windows OS

I've installed and condigured it in my Laravel 4 app. Now i want to run added tasks in a given period. What should i do to make this commad run automaticly? i mean what software or CLI to use? I'm using windows and want to run tasks locally.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.