Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

riddict's avatar

Run Laravel Command Using Link and Run in teh Background

Hello guys,

Is there any way to run a laravel artisan command using a link and run the script in the background so user won't have to wait the script finish processing. For example : I have a command called auto:like. User click on link localhost/run/like and execute the command auto:like in the background. Once finished, a notification send to user that the task have been completed. I can't use cron because my hosting provider not allowed me to do so. thanks.

0 likes
4 replies
Cronix's avatar
Cronix
Best Answer
Level 67

You should be able to. Just have a route to a controller method (your link) and issue the artisan command from there. https://laravel.com/docs/5.4/artisan#programmatically-executing-commands

You can set it to ignore user abort and the user can leave the page and it will run in the background. http://php.net/manual/en/function.ignore-user-abort.php

Depending on how long this script takes to run, you might also need to increase the php time limit: http://php.net/manual/en/function.set-time-limit.php

Not sure about your notification though, that would depend on how you want to notify them. Email? Popup on website? You can fire an event when the task completes and listen for it and handle it how you wish.

riddict's avatar

@Cronix thanks for your answer. Don't mind about the notification already implemented it using OnePush notification. So let's say user click on the link, how to display on the front end that the script is running, and user's browser stop loading (without the user stop it manually) while in the background the script is processing.

Cronix's avatar

Probably the best way is to use a queue for processing your long job. That way you don't need the above and the user doesn't have to wait.

The other way is probably with a javascript redirect, or have a popup show up telling the user that their job is being processed and click "this link" to continue. Link takes them to a different page. Otherwise it will sit there loading until it's completed its task. The job would still be running in the background though and user can close tab or browser and it will continue.

riddict's avatar

Yes, I think using javascriptis the best approach for this problem. thanks.

Please or to participate in this conversation.