Laravel 4 and crontab

In many projects, you need use crontab (cron jobs) to execute some tasks as sending email or delete waste record in DB. With Laravel Project, you can do this easier.

Be strong enough to let go and patient enough to wait for what you deserve

Be strong enough to let go and patient enough to wait for what you deserve

Create a command in Laravel 4:

With command above, Laravel will create a file named FirstCommand.php in directory  app/commands/

FirstCommand.php contains source code as below:

You should change this line:

E.g:  I changed to

Laravel Command supports you use parameters in your command. To add or remove parameters, you should look at getArguments and getOptions functions

And the most important function is fire() function. The command will execute source code that is wrote in that function.

I did the most simple changed to show you the easiest to understand Laravel Command. Please see file FistCommand.php after changing:

Next step, you need to register the command with Laravel CLI. So easy, you open app/start/artisan.php file, and add one line as below:

You are done creating Laravel Command. To test, you could use command below:

The output above mean you successfully register a command.

Finally, put your command into the crontab:

Add line (run command every 2 minutes):

That’s all. Thank you for talking time to read this.