Laravel 11 Crontab configuration with O2Switch server
To configure Laravel 11 to run cron jobs on an O2Switch server, you'll need to follow these steps:
-
Create a new Laravel cron key: Before configuring the cron job on the server, you need to create a new cron key for your Laravel application. You can generate a new key by running the following command in your terminal:
php artisan make:config cron
This command will create a new
cron.php
file in theconfig
directory. Open the file and set theschedule
key to a new instance ofArtisan\Scheduling\Schedule
:return new class() extends Schedule { /** * Define the application's command schedule. * * @return void */ public function schedule(Command $schedule) { // ... } };
Now, define your cron jobs inside the
schedule
method. For example, if you have a command namedMyCommand
that you want to run every day at 2 AM, you can define it as follows:public function schedule(Command $schedule) { $schedule->command('mycommand:name')->daily(); }
Don't forget to add the new
cron.php
file to your.gitignore
file to prevent it from being committed to your repository. -
Expose Laravel's cron endpoint: By default, Laravel's cron jobs are triggered by the
artisan schedule:run
command, which is not accessible via the web. To make it accessible, you need to expose the/schedule
endpoint. Open theroutes/web.php
file and add the following route:Route::get('/schedule', function () { Artisan::call('schedule:run'); return response()->json(['status' => 'success']); });
-
Configure the cron job on the O2Switch server: To configure the cron job on the O2Switch server, you need to create a new cron entry. First, you need to find the URL of your Laravel application. You can find it by running the following command in your terminal:
php artisan route:list
Look for the
/schedule
route and note down the URL. For example, if the output is:| GET|HEAD /schedule | web.php | App\Http\Controllers\ScheduleController@index
Then the URL would be
http://your-domain.com/schedule
.Now, log in to your O2Switch control panel and go to the "Cron Jobs" tab. Add a new cron job with the following settings:
* * * * * wget -qO - http://your-domain.com/schedule > /dev/null 2>&1
This cron job will run every minute and call your Laravel application's
/schedule
endpoint. The> /dev/null 2>&1
part redirects the output to nowhere to prevent any error messages from being displayed.Save the cron job and test it by running a command in your Laravel application that leaves a log entry. For example, you can add the following line to your
app/Console/Kernel.php
file:protected $schedule = function (Schedule $schedule) { $schedule->command('mycommand:name')->daily()->withoutOverlapping(); $schedule->command('inspire')->everyMinute(); $schedule->command('mycommand:name')->at('0 0 * * *')->writes(storage_path('logs/cron.log')); };
This command will write a log entry to
storage/logs/cron.log
every time it runs. Now, wait a few minutes and check the contents of the log file. If you see new log entries, then the cron job is working correctly.