Laravel scheduled command via CRON not running - php

I have a Laravel 5.1 site, and an artisan command set up that I can run manually without problems, but I cannot seem to get it to fire at a scheduled time.
I have a crontab set up on a digital ocean server, the contents of the file are
HELL=/bin/bash
MAILTO=""
* * * * * php /var/www/{domain}/artisan schedule:run 1>> /dev/null 2>&1
where {domain} is the actual domain.
In my kernel.php file I have the following
protected function schedule(Schedule $schedule)
{
$schedule->command('migrate:existing-users')->dailyAt('10:25');
}
There's nothing in the laravel log file that offers any clues, it just seems like nothing it happening.
How can I check if the cron is set up correctly?
EDIT:
It seems the cron tab was set up under the root user, so I have also set up another one under www-data, and put this simple line in both
* * * * * echo 'run this command every minute'
with my email address in the MAILTO= line, but nothing gets emailed to me on either crontab

Related

Cron tab doesn't run most of tasks

I setup several cron jobs to make things work. laravel scheduler works perfectly but my other cronjobs not working at all.
*/2 * * * * /usr/bin/php /var/www/cronjobs/index.php
when I run on the console /usr/bin/php /var/www/cronjobs/index.php it works properly. I checked executable php path with which php and gives me /usr/bin/php nothing wrong with path afaik. I tried to run php script as apache user www-data I opened crontab with crontab -u www-data -e and paste command there.. it didn't work too.
I also tried send dummy notify with crontab and it also didn't work either
dummy example
* * * * * /usr/bin/notify-send 'test'
both of them doesn't work. What am I missing here ?
The second command will not send notification as cron have no idea of your desktop environment.
The first command probably use some environment variables. So instead of run in command line you can try to create a script:
#!/bin/bash
source /path/to/user/home/.bashrc #you can try also .bash_profile
/usr/bin/php /var/www/cronjobs/index.php
and your cron to be like:
*/2 * * * * /path/to/script.sh

Command schedule in Laravel

I'm trying to a execute scheduled command every five minutes in background. I use this code
protected function schedule(Schedule $schedule)
{
$schedule->command('read:mail')->cron('*/1 * * * * *')->sendOutputTo(storage_path().'/logs/output.txt')->withoutOverlapping();
}
I suppose that this code is fine, when i use php artisan scheduler:run command works, but doesn't work every five minutes in background. ¿Any idea?
If you want to use the scheduler you need to add a Cron entry in your server, this line will call the Laravel scheduler every minute and do the tasks.
There is the line you need to add to your Crontab:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
In case you are using Ubuntu to edit your crontab you can run crontab -e and add the line on the bottom.
You can read more on the official docs about Scheduling.
If you are using Windows you can follow this Stackoverflow question to add a task to the Windows task scheduler.

Scheduling jobs in Laravel

I created a command in laravel "update:date" and "php artisan list" successfully lists it. I am able to execute it using "php artisan update:date"
Kernel.php:
protected $commands = [
'Snuba\Console\Commands\Inspire',
'Snuba\Console\Commands\UpdateDate'
];
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->hourly();
$schedule->command('update:date')
->everyMinute();
}
I configured it to run every minute as given above. Do I need to configure anything else ? I think laravel should automatically register it as cron task on ubuntu server.
I think laravel should automatically register it as cron task on
ubuntu server.
No, you have to start it like:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Starting The Scheduler
When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you
Reference
Another Answer
I just noticed:
Call to undefined method Illuminate\Console\Scheduling\Event::everyMinute()
I followed the documentation and thought this is a valid method call. I found that everyMinute() is no more valid in laravel 5+. So, I used cron expression instead.
Laravel's command scheduler allows you to fluently and expressively define your command schedule within Laravel itself. For more go through following link laravel task scheduling
If you have access to SSH
run command crontab -e
insert this line:
php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
which will run all your specified crons as there specified times.
If you have cpanel then under crontab add the above line on command
If ->everyMinute does not exist, please use ->cron('*/1 * * * * *') to set every minute. Or override class laravel.com/api/5.x/Illuminate/Console/Scheduling/Event.html
If cron job not working, run command crontab -e and add this line without single quote
'* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1'
After do this I think it works perfectly.

Error in cronjob via ssh server

I am trying to set connect cron job via ssh server. i am using command but displaying " no such file or directory ". How can i do this ? Here is my command
/1 * * * * /var/www/html/mysite.com/cron/cron.php
I assume that you are trying to run this cron every minute. Looks like sh script is not getting the correct location of your cron file. I would suggest adding these lines in your cron job.
CRONPATH=/var/www/html/mysite.com/cron
*/1 * * * * cd $CRONPATH; php cron.php

Custom Laravel Artisan command not available through cron job

I have custom Artisan commands that run locally as well as on my production server when I am SSH'd in, but are unavailable to any cron jobs. I've even tried running it as the user the cron job runs as and it works fine from my console.
When I run php artisan in the above settings, my custom commands are listed and available. However, they are not listed when I run php artisan as a cron job.
Furthermore, trying to run the custom command php artisan subjects:calculate as a cron job results in the following error:
[InvalidArgumentException]
There are no commands defined in the "subjects" namespace.
I was fighting with the same error and I found the solution.
First failed attempts
*/5 * * * * /usr/bin/php /home/mysite/public_html/artisan my:command
*/5 * * * * php /home/mysite/public_html/artisan my:command
Solution
*/5 * * * * /usr/local/bin/php /home/mysite/public_html/artisan my:command
Be sure to add the command to app/start/artisan.php file:
Artisan::add(new SubjectsCommand);
or if you are using the IOC container:
Artisan:resolve('SubjectsCommand');
Then run the CronJob from the folder of the app:
00 09-18 * * 1-5 php /path/to/yourapp/artisan subjects:calculate
or
00 09-18 * * 1-5 /usr/bin/php /path/to/yourapp/artisan subjects:calculate
At least for me that worked:
class Kernel extends ConsoleKernel
{
protected $commands = [
// Commands\YourCommand::class,
];
}
Just added my command to the list of command kernel.
You probable need to make sure artisan is running from the correct directory
cd /path/to/yourproject && php artisan subjects:calculate

Categories