In my project I've made a simple newsletter system where I want to send an email to all subscriber. But the problem I faced in production (cpanel hosting) I have to run laravel queue:work command in terminal.
Can anyone tells me how to handle queue:work in background in cpanel hosting...
It would be very appreciable...can anyone tell me how to handle this problem?
ANd using the laravel queue database connection
For sending the mail I'm using the shared hosting email smtp. Not using the mailchimp or mailgun etc...
@neeraj1005 You need to SSH into your server, and run php artisan queue:work as a detached process. You’ll also want to use something like Supervisor to make sure the process is restarted if it’s terminated for whatever reason.
However, if you’re using cPanel and this is a shared host, this will most likely not be possible. The reason being, with shared hosting, you share the resources of that server with other customers, so hosting company doesn’t want you triggering long-running processes and hogging up those resources. In which case, you’ll need to look at something “more”, like a dedicated host, VPS, or something.
@martinbean So, in cpanel I have to install supervisor and SSH?
Do you have any idea how this can be resolve...?
SO instead of database should I have to use sync
@neeraj1005 You’ve been told about three times now that this usually isn’t possible on shared hosting. So check with your host whether you can execute long-running processes like a queue worker, as I’m guessing not.
There’s no point continually asking how if it’s not possible.
it is an one time process if you run this commend the mail will send when we trigger, if you want to send emails automatically for example (need to send emails every week first day) you need to set cron job
in my case i create a command called Run:jobs
and add two Artisan command inside
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
class runJobs extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'Run:Jobs';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run Job in queue';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Artisan::call('queue:restart');
Artisan::call('queue:work');
}
}
then schedule Run:jobs everyMinute
$schedule->command('Run:Jobs')->everyMinute();
this will work for single queue with single worker for small prjects
@Snapey This is exactly what I was hoping to find, I am on godaddy cpanel hosting, its been working well thus far for all my needs for my laravel back-end for my portfolio, but having the ability to have a contact form so people can reach out to me for work I think is a good UX for visitors to my portfolio, but alas this feature has been difficult to setup on the cpanel server due to SMTP restrictions and non root/sudo permissions, but when there is a know how and a will to find a way, alas you encounter it. This is very good stuff sir thank you for sharing this link. Surprised it does not have more likes, but I guess only a few of us use shared hosting with cpanel....
So whats happening is, the first time it runs, it will start the queue worker in daemon mode, then on every minute, it will basically use the withoutOverlapping to only run it again if the previous one crashed/exited/no longer running. This essentially produces a supervisor like functionality. At worst case it will take a minute before the queue worker comes back up in case of a failure / memory limit hit, but in most cases the first thread will stay alive and work through the queue. This is a better way to accomplish queue:listen like functionality without supervisor.
The caveat is of course, you need to run queue:restart whenever you deploy new code so that the daemon worker will restart and get fresh app code.
if you are using shared hosting you don’t have root access to install Supervisor. However, you can achieve a similar result using Cron Jobs in cPanel to run background tasks.
🔹 Step 1: Access Cron Jobs in cPanel
- Log in to cPanel.
- Go to Advanced > Cron Jobs.
- Scroll down to "Add a New Cron Job".
🔹 Step 2: Add a Cron Job for Laravel, In the Command field, add: