4

I need to learn how to work with cron jobs in Laravel..As I can see the documentation does not specify this part.I have found a tutorial but it is about Laravel-3. Can you give me some advice on how to schedule a cron job running once a day..?Is there any tutorial about that issue?

My code so far is the following :

JobDaemon.php :

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class JobDaemon extends Command {

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'job-daemon';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Get all recent jobs once a day.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        $this->info('fired');
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return array(
            //array('example', InputArgument::REQUIRED, 'An example argument.'),
        );
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return array(
            //array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
        );
    }

}

I used the following command to set it up

php artisan command:make JobDaemon

And my artisan file is the following:

<?php

Artisan::add(new JobDaemon);

I get the following from my console...

johnnemo@johnnemo:/opt/lampp/htdocs/e-support-uop$ tail -f /var/log/syslog | grep -i cron
Jan  1 18:31:09 johnnemo crontab[4484]: (johnnemo) REPLACE (johnnemo)
Jan  1 18:31:09 johnnemo crontab[4484]: (johnnemo) END EDIT (johnnemo)
Jan  1 18:35:01 johnnemo CRON[5054]: (johnnemo) CMD (php /opt/lampp/htdocs/e-support-uop/artisan job-daemon)
Jan  1 18:35:02 johnnemo CRON[5053]: (CRON) info (No MTA installed, discarding output)
Jan  1 18:39:01 johnnemo CRON[5064]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -x /usr/lib/php5/sessionclean ] && [ -d /var/lib/php5 ] && /usr/lib/php5/sessionclean /var/lib/php5 $(/usr/lib/php5/maxlifetime))
Jan  1 18:40:01 johnnemo CRON[5076]: (johnnemo) CMD (php /opt/lampp/htdocs/e-support-uop/artisan job-daemon)
Jan  1 18:40:01 johnnemo CRON[5075]: (CRON) info (No MTA installed, discarding output)
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
John
  • 339
  • 2
  • 7
  • 17

2 Answers2

21

First you need to make sure your new command is up, so if you run

php artisan list

'job-daemon' must be in the list of commands

Then you test it:

php artisan job-daemon

Does it work? Cool, now you can set an editor of your own:

export EDITOR=nano

Open the crontab with it:

[sudo] crontab -e

Execute type to the the correct path for your php:

type php

And you should get something like

php is hashed (/opt/lampp/bin/php)

So your php executable is at

/opt/lampp/bin/php

This will open and editor with the current cron jobs, sudo is optional to open the root crontab, just add a line with yours:

25 10 * * * /opt/lampp/bin/php /whatever/directory/your/site/is/artisan job-daemon

This will run your command everyday at 10:25AM.

To execute it every 5 minutes you do

*/5 * * * * /opt/lampp/bin/php /whatever/directory/your/site/is/artisan job-daemon

Then you tail the syslog to see it running:

tail -f /var/log/syslog | grep -i cron

And you should see something like

Jan  1 10:25:01 server CRON[19451]: (root) CMD (php /var/www/<siteName>/artisan job-daemon)

In your command you cann't really print things on the screen, you won't see them printing, so to test you have to, for instance, save something to a file:

public function fire()
{
    File::append('/tmp/laravel.txt', "fired\n");
    Log::info('fired');
}

And then

tail -f /tmp/laravel.txt

To see the results in realtime.

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • I need to define an editor also..?e.g. sudo nano crontab -e..?What do I set for /var/www/ since I am working on my local server?I want also to test this out...How can I define it to run every 5 secs..?And how do I stop it..? – John Jan 01 '14 at 16:19
  • Just edited to answer some of your questions. So /var/www/ doesn't have to be /var/www/, it is the directory where your app is and where you have an `artisan` command to be run. – Antonio Carlos Ribeiro Jan 01 '14 at 16:27
  • I did as you told me but this don t seem to run..It runs one time and stops..Will this run 5 minutes after..?I use this tail -f /var/log/syslog | grep -i cron command to start it or php artisan job-daemon..?It displays fired only once in my console.. – John Jan 01 '14 at 16:33
  • It will execute that command every five minutes. You can do it `*/1` To see it running every minute. Keep looking at `tail` to see it running. But, look, this also depends a lot on your command, is it running and doing what you need to do? If your run it MANUALLY twice in sequence it does what you need it to do? – Antonio Carlos Ribeiro Jan 01 '14 at 16:37
  • If I run it two times I get fired and then again fired...Shouldn t I get lots of fired with the cron job..?I can see also in syslog the following : (No MTA installed, discarding output) – John Jan 01 '14 at 16:40
  • Where? Are you saving it to a file? If you are expecting to see it on your screen, you won't. It's running under another process. Append that fired to a file and you should see it many times. – Antonio Carlos Ribeiro Jan 01 '14 at 16:41
  • Ok...I udate the question to see what I get from my console...Are these correct or is there an error..? – John Jan 01 '14 at 16:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44278/discussion-between-antonio-carlos-ribeiro-and-john) – Antonio Carlos Ribeiro Jan 01 '14 at 16:47
1

I had a similar question to the OP, Antonio's answer got me most of the way there, but not 100%. I was attempting to schedule the job from the cPanel CRON Jobs page, and encountered either 404 errors or no error, but no success either. For me, the key was the following:

  1. Putty into my server, run the command:

    php artisan list

  2. Ensure my command was listed

  3. Run the next command:

    type php

  4. For me, the output was "php is hashed (/usr/local/bin/php)"

  5. When creating the CRON command, I had to use the qualified path. In addition, since artisan isn't globally available, you also have to use a qualified path to where it's available. For me, this was the laravel folder of my site. The last gotcha was that I had to prefix the actual artisan command with "command:"

In the end, I was able to schedule a laravel command via cPanel's CRON Jobs page using the following command:

/usr/local/bin/php /home/sitename/public_html/laravel/artisan command:TotalMadnessUpdateResultsCommand

Another common gotcha is not adding the following to your artisan.php file:

Artisan::add(new TotalMadnessUpdateResultsCommand);