How to automatically call laravel cron jobs while website is running - laravel

I am working with task scheduling in laravel. It's working well by using artisan command in cmd. But what's the problem with automatically calling the task on server. It's not running every minute.

https://laravel.com/docs/8.x/scheduling#running-the-scheduler
You will need to setup a cron job on your server that calls your scheduler

If you using Laravel Scheduler, you have to access in your server via ssh.
ssh user#<IP-address>
edit crontab file to call every minute php artisan schedule:run artisan command.
So:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
If it not start,try to restart the web server. If you are using nginx, for ex.: sudo systemctl restart nginx

Related

Laravel Forge Scheduler - Run Command

When using Laravel Forge's scheduler, you can trigger all the cronjob by e.g. running:
php /home/forge/default/artisan schedule:run
However, when wanting to execute a sole command, it will fail and says "fatal: not a git repository (or any of the parent directories): .git".
Via SSH, I would execute the command like this:
ssh to the server
cd path/
php artisan command:name
So how would I declare it in Laravel Forge's scheduler?
php /home/forge/default/artisan command:name
Does not work.
The solution is simply to remove the slash after /path/artisan - it is /path artisan.

Laravel queue:work runs forever

I have a weird issue with Laravel queue:work.
In my crontab I setup a job like described in Laravel Docs
* * * * * nginx php /path/to/site/artisan schedule:run >> /dev/null 2>&1
And in my app/Console/Kernel.php I setup this:
$schedule->command('queue:work')->cron('* * * * *');
On my production server php artisan queue:work runs for a few seconds and gets "killed". Thats what I expect.
On my dev box php artisan queue:work runs forever. So activating the cron job spawns php processes until the whole memory is filled up.
Both boxes are CentOS 7.4, production runs PHP 7.1 and dev runs PHP 7.2
As said in the comment below, I don't think that the cron command is the issue.
Running form ssh:
php artisan queue:work
on dev runs forever on production a few seconds.
You can try to run it once, this runs a single job and then quits the process.
php artisan queue:work --once=1
Or in your PHP code using the Artisan facade:
Artisan::call('queue:work', [
'--once' => 1, // Do NOT run it as a daemon (not a continuous function)
'--tries' => 1,
'--queue' => 'yourqueue',
'--timeout' => 0
]);

PHP-CLI Command Not Found

Now I just moved to inmotion from siteground and previously from bluehost. I am trying to set up my laravel(lumen) application on the server. I have been able to get everything else working but the scheduler which I just cannot seem to get to work.
This is the cron job php -q /home/xxxxx/xxxxx/artisan schedule:run which seem to run fine however it fails to execute the queue:work command.
I noticed on my previous hosting I had to edit the Illuminate\Console\Scheduling\Scchedule.php file like this:
return $this->exec("php-cli /home/xxxxx/xxxxx/artisan {$command}", $parameters);
using the regular php command did not work for some reason I had to use php-cli, however with my current hosting it says "command not found" whenever I try to use the php-cli command manually and all my cron job returns in my email is this:
Running scheduled command: php-cli /home/xxxxx/xxxxx/artisan queue:work > '/dev/null' 2>&1 &
I would like to know how I can fix this and get the scheduler to work.
Yes I have php installed. (v7.0)
Yes I have the php-cli package installed. (v7.0)
My VPS server uses linox OS.
You do write absolute route to php-cli command and artisan script to properly work:
/usr/local/php70/bin/php-cli /home/{username}/{path-to-app}/artisan schedule:run >> /dev/null 2>&1

Cronjob setup for laravel 4.2 in cpanel

I am using laravel 4.2. I have created command file for cron job and added it into artisan file. I tested it in command. Everything is working fine in localhost. In Cpanel server I gave command path like,
php /home/fridayburr/public_html/version1/artisan active:user 1>> /dev/null 2>&1
But cron job is not working.
This is how I did in my shared Hosting using CPANEL
Here CRON Task is set on UNIX, to run every minute.
Add the schedule call with appropriate time schedule.
laravel Schedule documentation

Laravel php artisan serve logs stored and start artisan serve automatically

id like to know :
Where Laravel php artisan serve command logs stored ?
How to automatically start php artisan serve when sever (Apache) starts ?
Laravel logs are stored at storage/logs
You don't need artisan serve, if you have Apache handling your HTTP traffic.
To the first question
Where Laravel php artisan serve command logs stored ?
Laravel server logs are stored in storage/logs folder ,which contains files for the server logs
To the second question
How to automatically start php artisan serve when sever (Apache) starts ?
If you need to automatically ,A very simple way to do this would be to use use laravel scheduling Artisan Commands using cron
Ensure cron is installed yum install vixie-cron (assuming you use centos). Start it with service crond start
Add laravel Schedule object vi /etc/crontab and add * * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1 ,to the cron schedule file .
Go to app/Console/Kernel.php file and add $schedule->command('foo')->withoutOverlapping(); where foo is the artisan command you would like to run in this case serve , so it should be $schedule->command('serve')->withoutOverlapping();
Restart cron deamon with service crond restart.
See this related question https://stackoverflow.com/a/34096590/1226748
I would definitely recommend NOT using artisan's server in production like EVER, it's for testing only. -- You would be WAY better off to use nginx as a reverse proxy and you can set whatever port you want that way, and have your angular app still connect to it.

Resources