Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Run command after server starts

I am working in PHP and the Laravel framework.

I have a command I want to run when I am working locally.

    exec("start chrome.exe http://127.0.0.1:8000");

I only need this command to run once when the server is started

I have tried placing it in a few spots, but when I run php artisan serve google chrome opens new tabs until I stop the server.

Where is a safe place to place this command so it executes only once after the server has started?

like image 284
Christian4423 Avatar asked Oct 20 '25 05:10

Christian4423


2 Answers

You should override the ServeCommand class (https://github.com/laravel/framework/blob/64fb6f745de496a2a114fdf896d3b60ab2119569/src/Illuminate/Foundation/Console/ServeCommand.php) and call your command in the handle method.

like image 161
cmizzi Avatar answered Oct 22 '25 20:10

cmizzi


I think I would create a bash script or something similar to handle this instead:

#!/bin/bash
# dev.sh
php artisan serve && chrome http://localhost:8000

Then you can just run dev.sh in your console.

like image 37
Westwick Avatar answered Oct 22 '25 20:10

Westwick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!