I have a Laravel project that is already deployed on a live server using Hostinger's web service. I have a task scheduled to run every minute that will basically check if there are organizations that have already expired subscription dates, which will revert their subscription type back to "Free". I tried it first on my local machine and it works great.
However, when I tried to implement this task scheduling in Hostinger, it doesn't work.
I followed Laravel's official documentation for running a Scheduler on a live server. Since Hostinger doesn't allow special characters, I created a bash file containing the artisan run command with special characters following their article.
Here's the content of my created bash file:
/usr/bin/php /home/u482004401/domains/caviom.org/public_html/artisan && php artisan schedule:run > /dev/null 2>&1
app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
CharitableOrganization::whereDate('subscription_expires_at', '>=', now())
->update([
'subscription' => 'Free',
'subscribed_at' => null,
'subscription_expires_at' => null
]);
})->everyMinute();
}
When I try to view the output of my cron job in Hostinger, it just shows a generic message of a list of artisan commands.
I have test data on my database that should be updated with this Cron job, but it did not change at all. Has anyone successfully tried setting up cron jobs on Hostinger for a Laravel project?
Looks like I wrote the wrong syntax. I fixed it by replacing my bash file:
/usr/bin/php /home/u482004401/domains/caviom.org/public_html/artisan && php artisan schedule:run > /dev/null 2>&1
with
#!/bin/sh
/usr/bin/php /home/u482004401/domains/caviom.org/public_html/artisan schedule:run
Once I saw that the output is now what I expected, I then returned back the
/dev/null 2>&1
Related
I must update my currency rate and I have this in my Plugin.php:
public function registerSchedule($schedule) {
$schedule->call(function () {
$url = "https://cbu.uz/ru/services/open_data/rates/json/";
$json = json_decode(file_get_contents($url), true);
file_put_contents("currency.json", json_encode($json[0]['G4']));
})->everyMinute();
}
I ran my cron job in cPanel
/usr/local/bin/ea-php72 /var/www/u1041398/public_html/agroparts.uz/artisan schedule:run >> /dev/null 2>&1
The path is correct I checked the PHP version it is correct also. My cron codes in plugin.php are correct also but it is not updating the currency rate. Is this command above correct to run cron job? I checked my code without cron and it worked successfully.
Cron job was not showing mistake. So, I created console command to update currency rate and run it and it showed
file_get_contents(): https:// wrapper is disabled in the server configurati
on by allow_url_fopen=0
So I used curl to get currency rate and worked
I am using Task Scheduling from Laravel, on local env. and till now I test it with php artisan word:weeklyUpdate , but I want to check if the Cron Job run automatically on a specific date, like in my code.
protected function schedule(Schedule $schedule)
{
$scheduler = new LkpSchedulerUpdateDate;
$scheduler = $scheduler->first()->toArray();
$schedule->command('word:weeklyUpdate')->weeklyOn($scheduler->date, $scheduler->time);
//ex: weeklyOn(3, 05:49:00)
}
Create a schedule that runs just some minuts after the current time (let's say 5 minuts from now), wait, then check the results.
Edit:
It's because when you run php artisan word:weeklyUpdate` you execute the command directly.
The scheduled task your wrote is equivalent to execute php artisan word:weeklyUpdate in console every week.
Also, in your locale did you activated the cronJob scheduler?
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
I did a maintanace on my laravel app by php artisan down. Then again after 15 minutes I did php artisan up.
But the scheduled jobs have not still resumed. They are not being executed as it was before.
Schedueled jobs are not being executed even when I manually give the command. ex: php artisan my:command
I think by making logs of your cronjobs will help you.
First Method
remove this part from you cronjob
>> /dev/null
Now add log file path in you cronjob
* * * * * php /var/www/html/project/artisan schedule:run >> /var/www/html/project/mylog.log 2>&1
create a log file and this file 777 (writable) permission.
so you can check what actually happening
Second Method
Or you can use method appendOutputTo() in your kernal.php
$schedule->command('inspire')->everyMinute()->appendOutputTo($filePath);
$filePath is a path of a log file you can give a public path or storage path
I've set up a console command with a handle() function like this:
public function handle()
{
$fileSystem = new Filesystem;
$fileSystem->cleanDirectory('storage/app/public/tmp');
}
And in the console kernel I set up the command:
$schedule->command('cleanupfiles:tmp')
->everyMinute()
->sendOutputTo(storage_path('logs/taskoutput.log'));
The superuser's crontab has the following entry:
* * * * * php /var/www/website/artisan schedule:run >> /dev/null 2>&1
I can see the task scheduler getting executed every minute by looking at the /var/log/syslog, so cron does it's job, but the folder contents are not cleaned. When I run the task directly on the terminal by: php artisan schedule:run I have the same effect; no files are deleted. Finally when I run the schedule with sudo php artisan schedule:run I see it works, files get deleted and output is written to taskoutput.log.
How can I solve this so the task runs with necessary permissions? Or is there anything else I miss here? Thanks.
I'm trying to follow the Laravel Documentation on how to Run Cron Jobs,
and I want to add this
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
But I don't know how to add it to Heroku.
I created this Scheduler which runs once a minute and is part of your own app.
https://gist.github.com/robbydooo/65bf341ea0f4081150b945bfb1d38d3c
It creates a new Dyno type called Scheduler which you start one of.
Make sure you run jobs on your queue to avoid this scheduler over running once per minute.
To use this scheduler with Laravel 5.4+ add this file to /app/Console/Commands/RunScheduler.php
Register this file in app/Console/Kernel.php
protected $commands = [
…
Commands\RunScheduler::class
…
]
Add this line to your Procfile:
scheduler: php -d memory_limit=512M artisan schedule:cron
Heroku has a cron scheduler addon that you can use for scheduled tasks.
You can install it like this:
$ heroku addons:create scheduler:standard
Have a look at this article for more information.
It's been a while since this question was asked but recently I've had a similar issue and was able to overcome it using this blog post:
https://dev.to/autoidle/run-laravel-scheduled-jobs-on-heroku-2ah6
You can basically create a console command using this artisan generator:
php artisan make:command SchedulerDaemon
then you can edit app/Console/Commands/SchedulerDaemon.php and edit some lines as described below:
...
class SchedulerDaemon extends Command
{
...
// Change $signature value
protected $signature = 'schedule:daemon {--sleep=60}';
...
// Change $description value
protected $description = 'Triggers scheduler every minute or --sleep seconds interval';
...
public function handle()
{
// Change handle() function body to this:
while (true) {
$this->info('Calling scheduler');
$this->call('schedule:run');
sleep($this->option('sleep'));
}
}
...
}
Then, add this line to Procfile:
scheduler: php artisan schedule:daemon
And remember to enable scheduler process on heroku dashboard or run:
heroku ps:scale scheduler=1
I hope it becomes helpful to others in the future!
Cron To Go is an add-on that allows you to run Laravel's task scheduler every minute (* * * * * in cron). Simply install the add-on, add a job with * * * * * as the schedule and php artisan schedule:run as the command, sit back and relax! If your Laravel logs don't show up for the scheduler, there's a quick fix for log routing described here.
A free alternative can be to use the free worker dyno and configure it as follows in the Procfile
web: vendor/bin/heroku-php-apache2 public/
worker: bash -c "while [ true ]; do (php artisan schedule:run &); sleep 60; done"
This command creates a while loop for run schedule:run every minute.
Then you must exec heroku ps:scale web=1 worker=1 to enable worker dyno.