How to apply laravel scheduler on Azure app service
As you may know, all the data in app service server will not persist except for /home directory. And every time you deploy something on this server, it will be reset as if it were starting from brand new status.
There are convenient way to handle this situation, we can use 'startup.sh' script. This script runs everythime we deploy something on the server.
Since PHP service and Nginx are already installed by default, cron is not the case. Therefore, we need to install it with our script to ensure that the cron service is up and running when deploy our new code.
This is the part of the sample script.
echo "Installing cron"apt update -qqapt install cron -yqq echo "Adding a cronjob for laravel scheduler"(crontab -l ; echo "* * * * * /usr/local/bin/php /home/site/wwwroot/artisan schedule:run >> /home/site/wwwroot/storage/logs/cron.log 2>&1") | crontab - # make sure the cron service is up and runningservice cron restart
I created a simple health checker command for my service, and it will send a simple text message to my slack channel.
routes/console.php
<?php use Illuminate\Support\Facades\Schedule; Schedule::command('app:health-checker')->hourly();
That's it! we can use this for anything that need to be run periodically.
In addition, if you want to test your command in your console.php, Laravel provides a pretty simple and straightforward artisan command.
php artisan schedule:test
You can choose any command that you want to test from the following prompt after executing this command, and it will run immediately.