Good day all. I have this persisting problem with a laravel app I created. I want to send bulk emails to subscribers. I want to queue the emails (jobs)so that they won't slow down my app. I am using a shared hosting account. Without the queue process, the mails work work fine. However, without it, they do not work.
I am using database as Queue Connection and I have the "jobs" table set up in my database. I guess up till this point, everything is working perfectly well because anytime I send the mails, I can see the jobs in the database. The problem is that I can't seem to make the queue:work command on the shared hosting unlike on my system (locally).
Furthermore, I created command using php artisan make:command cronEmail, and inside App/Console/Kernel.php, I set up the schedule method as follows
protected function schedule(Schedule $schedule)
{
$schedule->command('queue:work --tries=3')
->cron('* * * * * *')
->withoutOverlapping();
}
I then created a cron job on my cpanel as follows:
php /home/myrootfolder/mywebsitefolder/artisan queue:work >> /dev/null 2>&1
Yet, still I don't get any result. The mails are not getting sent.
My Website files are set up as follows:
Inside the root folder of the cpanel, I created a new folder called "mywebsitefolder" where I put all my laravel files except for the "public folder". The contents of the public folder are placed inside the root folder's "public_html". I then edited my index.php accordingly. It's shown below:
require __DIR__.'/../mywebsitefolder/vendor/autoload.php';
$app = require_once __DIR__.'/../mywebsitefolder/bootstrap/app.php';
So, could it be that I am not pointing to my "artisan" correctly or I am getting everything completely wrong?
Please, if anyone knows a better way of doing this or where I am getting it all wrong, I'll be glad. Thanks in advance.
You can easily from your Cpanel you will find terminal open it and run this command
nohup php artisan queue:work --daemon &
you can find the terminal in the advanced section
it will run your queue in the background
make sure when you run the command above to be in your project app path
Related
I am having issue of running laravel scheduler to send mails in the queue
The setup is as follows: Laravel 5.7
I have configured the scheduler (App/Console/Kernel.php) like mentioned below
protected function schedule(Schedule $schedule)
{
$schedule->command('queue:work --tries=3')->everyFiveMinutes()->withoutOverlapping();
}
The db is set-up as per laravel docs. As soon as I click the link in my UI, I can see the entry in the db.
The .env QUEUE_CONNECTION=database and the same setting in Config/queue.php
(if i change the database to sync, it works perfectly)
My cron job in the server is as follows ( i just tried to log the cron)
/usr/local/bin/php /home/XXX/YYY/artisan schedule:run 1>> /home/XXX/public_html/junk/cron_log.php 2>&1
I can see the cron logs getting updated every five minues but
"No scheduled commands are ready to run"
Exactly the same code and settings last night worked(before going to bed). I had tested for more than
40 emais send attempts and the db entries were getting deleted. I only tried to save the scheduler with everyFiveMinues() but now it is not working.
I can understand mails reaching slowly but why the db entries were not deleted like last night?
this may be useful to other who are using Laravel 5.7, shared hosting Godaddy.
The above issue of dispatching email jobs was not executing ( I mean the cron jobs are running but database entries are not cleared. The issue seems to be with
->withoutOverlapping();
After I have deleted this method, I am now seeing the cron_log entries correctly and I have also received mails. My cron_log entries are as seen below
Running scheduled command: '/opt/alt/php71/usr/bin/php' 'artisan' queue:work --tries=3 > '/dev/null' 2>&1
I am guessing the method withoutOverlapping() has problem in cron execution. I have not changed anything in the code.
In my Laravel application, I have created a scheduled task, as defined in a previous question that I asked. It essentially grabs data and updates a few tables.
I followed a help article on 1&1 about creating cronjobs.
To summarise this article it said the following.
SSH to your server, that you want to set up the new cronjob on, use whereis php to find the location of your PHP Binary, then use the crontab command to access the crontab and add a new job.
This will require the PHP path and the path to the script that is to be run.
The issue is when I run that command I get the following.
No crontab for [username]
So, I then Googled and it seems some shared hosts don't allow you to use or even access the crontab.
On my shared hosting server the path to php-7.1-cli is as follows /usr/bin/php7.1-cli
Now, Artisan is part of Laravel and requires a higher version of PHP than what is found in \usr\bin\php
Given this, for the cronjob I thought I'd be able to do something like this:
* * * * * /usr/bin/php7.1-cli /path/to/artisan schedule:run >> /dev/null 2>&1
Or
* * * * * /usr/bin/php7.1-cli /path/to/mynewable/artisan schedule:run >> /dev/null 2>&1
My folder structure is as follows:
root
/mynewable
But when I run either of these commands, nothing happens.
1&1 also has an inbuilt cron job creator but this only allows me to specify a URL path to a file that would run the necessary script.
Am I being prevented from using crontab? If so, giving a URL seems impractical as the user wouldn't need to validate, but making a path in web routes to run the Laravel scheduler seems a bit of a security hole.
You can create a executable cron.php in your public folder of laravel.
exec("/usr/bin/php7.2-cli -f /homepages/x/xxxxx/htdocs/artisan schedule:run 2>&1", $out, $result);
if(!empty($out)) {
echo implode("<br>\n\n", $out);
}
Now you can call http://example.org/cron.php
Of course you can put the cron.php in a protected directory so no one can call the cron.php.
I am trying to setup a Cron job which will run after every minute and perform some task. I am using Laravel 5.5 and my site is hosted on Godaddy with shared hosting plan.
First I have implemented schedule method in app/Console/Kernel.php file like below:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$video = new Video;
$video->title = 'sample title';
$video->save();
})->everyMinute();
}
Then I have created a Cron Job in relevant section from Godaddy cPanel section. This section looks like below:
I have also setup to send me email every time this tasks runs but nothing is happening. No email, no new entry in Videos table. Although other parts of application are configured correctly and working fine.
As far as my guess is, there seems to be something wrong with the path to artisan file that is given in php command. I have tried different combinations but no luck.
I have fixed the issue. The problem was in the command. Firstly I had to put ~ at the start of the path to artisan file. Second I had to enter the absolute path to the php executable. So the final command that worked for me is:
/usr/local/bin/php ~/public_html/fifa/artisan schedule:run >> /dev/null 2>&1
I am using Laravel Forge and this works for me:
php /home/forge/project_directory/artisan schedule:run
Is /public_html/fifa the path to your project? Try running artisan directly from your project directory:
php /path_to_project/artisan schedule:run
I have attempted to create a cron job in my CakePHP 2.x application. But all of the resources I have read online seem to be either doing it completely different to one another with little consistency or explain it in very complex terminology.
Basically I have created the following file MyShell.php in /app/Console/Command
<?php
class MyShell extends Shell {
public function sendEmail() {
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->from('cameron#driz.co.uk');
$email->to('cameron#driz.co.uk');
$email->subject('Test Email from Cron');
$result = $email->send('Hello from Cron');
}
}
?>
And I want to say run this code at midnight every night.
What do I do next? As the next part really confuses me! I have read on the Book at: http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html that I should run some code in the terminal to make it do it at a certain time etc. And I can set these up using my hosting provider rather easily it seems.
But I'm rather confused about the Console directory. What should go in what folder in here: https://github.com/cakephp/cakephp/tree/master/app/Console
/Console/Command
/Console/Command/Tasks
/Console/Templates
Also noticed that many of the files are .php (e.g. my Shell file is also .php), but according to documentation I've read for Cron jobs, the executed files should be .sh?
Can anyone shed more light on this?
And what would the code be to call that command?
e.g. would presume this is incorrect: 0 0 * * * cd /domains/driz.co.uk/html/App && cake/Console MyShell sendEmail
Thanks
No. There is no way to do it just in PHP. But that doesn't matter, because crons are easy to set up.
In that article you linked to, you still have to set up a cron - the difference is just that you set up a single cron, that runs all your other crons - as opposed to setting up one cron per job. So, either way, you have to learn to create a cron.
The instructions depend on your server's operating system and also what host you're with. Some hosts will have a way to set up cron jobs through a GUI interface like cPanel or something, without you having to touch the terminal.
It's usually pretty easy to find instructions online for how to set up cron jobs with your host or server OS, but if you're having trouble, update your question with your host's name, and your server OS and version.
Also ---------------------------------
Often in cron jobs you'll be running a shell script (.sh). But don't worry about that for this case; your's will end in .php.
Re: the directory structure:
/Console/Command is where your new file should go.
If you're doing a lot of shell stuff, you may want to abstract common code out into the /Console/Command/Task folder. Read more about that here. This probably won't be needed in your case.
/Console/Command/Templates is where you can put custom templates for the Cake bake console - don't worry about that for now.
If I've only got a couple of cron jobs to run, then I create just one file called CronJobsShell.php, and put them all in there.
Really, you should read Cake's documentation on shells from start to end. It will give you a nice picture of how it all hangs together.
This can be done very easily by the following steps -:
1) Create a shell let's say HelloShell.php in Console/Command
<?php
class HelloShell extends AppShell
{
public function main()
{
//Your functionality here...
}
}
?>
This shell can be called by Console/cake hello
2) Write the command crontab-e .This will open up the default editor or the editor which you select Now as we want that our shell should run after at midnight write:-
0 0 * * * /PATH TO APP/Console/cake hello
For better understanding refer https://www.youtube.com/watch?v=ljgvo2jM234
Thanks!
I'm running an application where I need to execute the cron job, once a form is submitted,contains more than 1 lac records in a loop(its on-demand execution of cron job and deletes the job once finished).
I'm using php, I tried to configure the job through cpanel as a test and it works...the command was.."/ramdisk/bin/php5 -f /home/user/public_html/domain/cron.php -q", it worked fine.
But when I tried to run the same job with the exec() of php, like I wrote this whole command to a file (I do not know where the original crontab file is located otherwise could have written to it, please suggest a way to find its location) and ran that file like "exec("crontab /home/user/cron/Feed_cron"); ", in this cron is not working...
I doubted whether the crontab command will not work for particular user.. I tried it from the "root" user....which is the root user....tried the command via SSH or Putty and the response was "command not found". Please find a solution for this, also please give me an idea whether my concept works fine...
please suggest a way to find its location
Try
whereis crontab