Task schedular of laravel not working properly on server - php

I am able to execute individual artisan commands on server using putty cli. for eg.
php artisan inspire
and other custom commands related to database, they work fine while using putty. Also i put them in kernel.php 's schedule funtion like this
protected function schedule(Schedule $schedule){
$schedule->command('customcommand:execute')->everyMinute();
}
when i run this command using putty
php artisan schedule:run
it also works fine.
problem is that i am not able to execute schedule:run command via servers cron job.. command looks like this on server
php -d register_argc_argv=On /path/to/artisan schedule:run >> /dev/null 2>&1
funny thing is i am able to execute individual commands via servers cron job
i.e.
php -d register_argc_argv=On /path/to/artisan customcommand:execute >> /dev/null 2>&1
works as well...
only that schedule command is not working.
also it does not show any errors..
also if I dont add '-d register_argc_argv=On', i get exception 'ErrorException' with message 'Invalid argument supplied for foreach()
again here by server i mean cPanal, i have added this command under 'cron jobs'

The ini directive register_argc_argv was disabled on the php.ini and I needed to enable it explicitly on each call in order to accept argc and argv parameters. Luckily I easily solved this problem using exec instead of command
$schedule->exec('php -d register_argc_argv=On /path/to/artisan sms:bulk-send')
Use your command instead of sms:bulk-send Then it will be work.

* * * * * /usr/local/bin/ea-php71 /home/your_domain/artisan schedule:run >> /dev/null 2>&1

first ,
which php
to get which php you're using now
then change your cron job command like
* * * * * /path/to/your/php /path/to/your/project/artisan schedule:run
specify which php and project you are using
for example * * * * * /usr/local/bin/php /home/user/project/artisan schedule:run
it works for me

Related

Laravel task scheduler not running

I have the following method in my Kernel.php (the one under Console of course):
protected function schedule(Schedule $schedule)
{
$schedule->exec("touch lorem.txt")->everyMinute();
}
And I have the following cronjob added through the cpanel:
* * * * * cd /home/oeit/oe && php artisan schedule:run >> /dev/null 2>&1
I am supposed to see a lorem.txt file in the disk. However when I search for it using find / -name "lorem.txt" the file doesn't appear, which makes me believe that my cronjob is not working properly. I am on a shared hosting.
How do I fix this?
I had to specify the full path for the php executable:
* * * * * cd /home/oeit/oe && /usr/local/bin/php artisan schedule:run >> /dev/null 2>&1
If you're on shared hosting, it's entirely possible that it is running, but you don't have access to use $schedule->exec on the server.
I would take a look in the storage/logs/laravel.log file to see if you are getting any errors.

Laravel - Task Scheduling

I'm using Laravel 5.4 on a local WAMP Server. I wanted to perform a Task Scheduling but I'm not sure if I really understood how this works.
I created a command cronEmail and in the handle() function added code where I would get an Email.
In Kernel.php I added this:
protected $commands = [
'App\Console\Commands\cronEmail'
];
...
protected function schedule(Schedule $schedule)
{
$schedule->command('send:email')
->everyMinute();
}
So basically I want to get an email every minute. But how do I start this?
I tried entering:
php artisan schedule:run >> /dev/null 2>&1
or even
php C:\wamp64\www\seo-parser\artisan schedule:run >> /dev/null 2>&1
on my cmd but I always get:
The system cannot find the path specified.
If i enter php artisan schedule:run it will actually send an email but only once.
Did I understand the whole concept wrong? How do I do this properly?
Thank you,
Patrick
As stated in the official Laravel documentation you need to add the following line to your crontab.
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
You do this by typing "crontab -e" in the console. Paste the above line and replace the "/path-to-your-project" with the path to your project.
This results in a cronjob wich calls the "php artisan schedule:run" command every minute.
This requires you to run Linux though. If you need an alternative to crontab when running Windows you can start by reading here.

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.

Symfony2 cronjob environment not found

I am trying to run a Symfony2 command with a cron job but I get an error that the environment is not found. Here is my cron job:
* * * * * /usr/local/bin/php /usr/lib/myApp/app/console >> /usr/lib/myApp/forumLog.txt 2>&1
For now I am just trying to make app/console work and the expected output is a list with all commands. The error that I get is:
[Symfony\Component\Debug\Exception\ContextErrorException]
User Error: The environment was not found
Do you have any idea what is wrong and what is the correct way to run symfony2 commands through cronjob?
In my own Symfony-console running cronjobs, I usually have cron run a shell script, that first changes into the apprppriate directory, and then runs the console command.
Here's an example that has been running for a year or two:
File: /etc/cron.d/systemChecks (run a shell script as user: www-user)
10 7,19 * * * www-data /var/www/dir.../bin/liipMonitor.sh
File: /var/www/dir.../bin/liipMonitor.sh
#!/bin/sh
# Running at 7:10 and 19:10
cd /var/www/dir.../
bin/console --env=prod monitor:health --group=cli -q
I put the cron setup into their own files in /etc/cron.d but much the same would apply in any other crontab file. The shell script changes directory to the base directory of the project, and then runs bin/console.
Set the --env parameter in the cronjob command, like that:
* * * * * /usr/local/bin/php /usr/lib/myApp/app/console --env=prod >> /usr/lib/myApp/forumLog.txt 2>&1

Run PHP script in crontab only works when exporting to a file

On CentOS release 6.5 (Final):
I know that usually I just need to use the following command in crontab to run a php script.
0 * * * * /usr/local/bin/php absolute_path_file_to_the_script.php
But, recently, it stopped working. The only work around is to use the following command
0 * * * * /usr/local/bin/php absolute_path_file_to_the_script.php > log
But I would rather not to output anything to log for now.
So, I even tried
0 * * * * /bin/sh -c "/usr/local/bin/php absolute_path_file_to_the_script.php"
But the above commend is again not working in crontab (it works if I type in the shell directly).
And I am sure that the above command did run in crontab for a second with
ps ux, and then it stopped executing.
Any ideas on how to run the command properly without logging?
Try run that command (only php, without cron settings) from terminal and show result
Both answers from Marc and Greg work:
> /dev/null
or
> /dev/null 2>&1

Categories