run cron job in octobercms cpanel - laravel

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

Related

Setting up Cron jobs for Laravel on Hostinger's hpanel

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

Laravel cron job on cpanel is not running

I'm trying to run a cron job for task scheduler in laravel 8 on cpanel
here is the command im entering in cpanel cron jobs page
usr/local/bin/php /home/webservername/public_html/laravelappfolder/artisan schedule:run >> /dev/null 2>&1
the cron job is set to run once every minute
and in the Kernel.php looks like this(the schedule function part):
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$d = DB::table('users')->where('userid', '=', 5)->first();
$newAge = $d->age + 1;
$d->update(['age'=> $newAge]);
})->everyMinute();
}
I'm doing a basic call to a function every minute, but nothing in the database is changing. I checked disable_functions thing but that line doesn't exist in my php.ini. I also tried doing Log::info('test') but no new logs to be found.
I also tried creating a command and calling it with its signature name and registering it in the $commands array, but same thing cron job not working

Check if Task Scheduling works on local, Laravel

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

Cron job of my custom command in Laravel 4.2

I know there is Laravel 5.* now but for many reasons I'm using Laravel 4.2
I have a custom command, and in the fire() method, I import a file and seed the information in my database, so my class is some like this
class SeedDataFile extends Command {
protected $name = 'import:file';
public function fire(){
$command = 'mongoimport --db things --collection users --type csv --file file.csv --headerline';
$result = exec($command, $output, $return);
}
}
I want the file that is seeding data, i.e. every 12 hours (considering the file every 12 hours is changing with new data), but as an user I don't want to type the command in my terminal:
php artisan import:file
..every 12 hours (I just want to upload the new file to my project).
So the cron job is where I do all the work, but I don't want to do this:
crontab -e
and edit the file
I want to setup the schedule of my command in one class o somewhere in the code, and automatically the custom command is running everyday until a determined date.
Is this possible? Or I have to configure the crontab file?
You could take a look at this package, description says that it does exactly what you want
https://github.com/Indatus/dispatcher
As is Laravel 4.2 does not offer this type of functionality. In fact you'll need to set up 1 cron job for this functionality no matter the version you're using.
Newer versions of Laravel do indeed provide this functionality, but they still require you to set up 1 cron job (scheduler on Windows) at highest frequency available for Laravel's command php artisan schedule:run.
If you want to have scheduling of your commands in your code (rather than cron file) you could replicate what newer versions of Laravel do.
1. you would create a "master" command, let's call it TaskSchedulerCommand and let's say it's signature is php artisan schedule.
2. you would create a cronjob at highest frequency on the TaskSchedulerCommand like this: * * * * * php /path/to/artisan schedule`
3. you would write all the scheduling logic of other commands within the TaskSchedulerCommmand

How to set cronjob to send emails during an condition fails? and How to test it in local ubuntu system?

I need to perform a cronjob which sends emails when a perticulor condition fails. Cronjob should run everyday. And How to test the cronjob in local machine (I am using ubuntu 12.04).
I have already setup the sendmail for php. mails are going from magento.
Thanks in advance.
First, to run PHP from a command line on ubuntu:
sudo apt-get install php5-cli
The way to handle exceptions in PHP is
try {
} catch (Exception $e) {
mail('test#example.com', 'Error subject of thing failed', "Body of thing failed\n".print_r($e,1));
}
If you want the script to stop once the error has occurred, just put a die() or exit after the mail() call.
But your question is a little light on details since we don't know what's failing or where. It could just be that your PHP code is checking for X in the OS/filesystem, then all you need is to call mail() when the condition is found.
As far as the cronjob side goes you need to do this:
crontab -l > crontab.txt
Use editor of choice to open crontab.txt
Add a line at the bottom of crontab.txt (example below)
crontab crontab.txt
Then run crontab -l | less to review the new crontab to make sure it is correct
This will run every day at 1:45am server time
45 1 * * * /usr/bin/php /path/to/script/to/run.php
http://en.wikipedia.org/wiki/Cron#Examples
You can test the script by running it manually or setting the cronjob to start in the near future instead of 1:45am

Resources