Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Laravel includes âSchedulingâ to handle cron jobs. You can easily schedule them from inside Laravel using an expressive and easy to use syntax.
Scheduling
The âApp\Console\Kernelâ class handles all the scheduling in Laravel.
You should NOT put any logic in here though. This method should only be used to set the schedule. To actually define what to run you should create a new artisan command for each task you want to run. You can create a new command by running php artisan make:command. If your command is called "HourlyUpdate" run php artisan make:command HourlyUpdate.
It will create a new file inside app/Commands. Open it and edit its âsignatureâ. It will be the name used from the kernel class and the command line to call your task.
Edit its description. It should be a human-readable explanation of the purpose of the command.
Edit its handle. The handle is the method that executes the task.
Now open âKernel.phpâ in âapp/Consoleâ and add our newly created class to the âcommandsâ array:
https://gist.github.com/01c903d943a91bbca4b4911489bb6b46
Edit âscheduleâ to schedule our tasks. For example everyMinute() will run the task every minute while everyHour() will run it every hour. You can also specify a specific day of the month with monthlyOn(1, '00:01')Â . To find a list of all available options check [the official documentation][4]
You can also tell Laravel to print the output of the task to a file appendOutputTo(PATH).
For our example, weâll be running our task at the beginning of every month. Weâll also be saving the output to a file for debugging purposes and we want to make sure that a task doesnât start if there is another task running.
https://gist.github.com/5cb1a689c4b6dbe691e37ce4a0f241c0
Testing
You can test your cron job by manually calling it from the Terminal. You can list all your custom action and the default artisan actions using
https://gist.github.com/1b64da5a8797667df698f2d9c6a7ff5b
It will return a list of all available actions.
https://gist.github.com/f31b531ac186430fb08bc0bbf23a8a2f
âmonthly:quotaâ is our custom action. We can call it using php artisan monthly:quota.
Adding to Cron
Finally, you need to actually go to the crontab in your Linux installation and tell the system to call Laravel every minute.
Laravel will than evaluate all the cronjobs and decide if it needs to schedule something for execution or not. If it doesnât need to perform any action it will return straight away.
Open the crontab using the Terminal. It is usually located in /etc/crontab and add the following line, making sure to write in the path to your Laravel Project.
https://gist.github.com/03f2d39e6beb05d98f481a5cc902a0a7
Weâre also ignoring the output since Laravel already allows us to configure what to do with the output.
As always with schedules be mindful of different time zones.
Conclusion
Laravel makes it extremely easy to schedule tasks to run at specific intervals with an immediate syntax and many configuration options.
Iâve just launched Markdown Love in less than 24 hours, an API to turn any article on the web into markdown removing all the clutter, using Laravel.
You can find me on my Website and on Twitter.
Implementing a cron job with Laravel 5 with Scheduling was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.