I'am developing a project in laravel 5.3 where in a particular command that I created prints "Hello".
I want this command to be called after every 1 minute.
I know in linux you can use cron to schedule the tasks but I'am using windows so I used the windows taks to schedule a .bat file to run after every 1 minute,but it doesnt work.
My .bat file looks like this
cd c:\Users\User\Desktop\alerts
C:\wamp64\bin\php\php7.0.10\php.exe artisan schedule:run 1>> NUL 2>&1
The schedule function in the Kernel.php looks like this:
protected function schedule(Schedule $schedule)
{
$schedule->command('custome:command')->everyMinute();
}
And the actual task that will be called looks like this
public function handle()
{
echo 'Hello';
}
It doesn't give any output. It should automatically print "Hello" after a minute right? But it doesn't. Where is the output shown?
I am going to simplify the process for you.
The content of your .bat file should rather be
"C:\wamp64\bin\php\php7.0.10\php.exe" "c:\Users\User\Desktop\alerts\artisan" "schedule:run"
NB: According to the directories from your question.
Create a task and call this .bat file as an "Action" in the Task Scheduler on your windows.
public function handle(){ logger("Hello"); }
The logger function will output your message to the default laravel log file "laravel.log" for u to see at the bottom.
Hope it helps others too.
Related
I am trying to setup a Cron job which will run after every minute and perform some task. I am using Laravel 5.5 and my site is hosted on Godaddy with shared hosting plan.
First I have implemented schedule method in app/Console/Kernel.php file like below:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$video = new Video;
$video->title = 'sample title';
$video->save();
})->everyMinute();
}
Then I have created a Cron Job in relevant section from Godaddy cPanel section. This section looks like below:
I have also setup to send me email every time this tasks runs but nothing is happening. No email, no new entry in Videos table. Although other parts of application are configured correctly and working fine.
As far as my guess is, there seems to be something wrong with the path to artisan file that is given in php command. I have tried different combinations but no luck.
I have fixed the issue. The problem was in the command. Firstly I had to put ~ at the start of the path to artisan file. Second I had to enter the absolute path to the php executable. So the final command that worked for me is:
/usr/local/bin/php ~/public_html/fifa/artisan schedule:run >> /dev/null 2>&1
I am using Laravel Forge and this works for me:
php /home/forge/project_directory/artisan schedule:run
Is /public_html/fifa the path to your project? Try running artisan directly from your project directory:
php /path_to_project/artisan schedule:run
I'm trying to run a Helper function (App\Helpers) on a scheduled timer using Laravel (for testing purposes I have it running once every minute). I'm using Laravel 5.3.
This is my schedule function in my Kernel.php...
protected function schedule(Schedule $schedule)
{
$schedule->call(function()
{
// Calling this function should write a new file with a random number in it.
// I know this works perfectly fine outside of the scheduled task because I
// call it in other places, and it works)
FileEdit::UpdateFile();
})->everyMinute();
}
The issue is that the FileEdit::UpdateFile() part is NOT ever being called by the laravel at the designated time intervals.
Are you running a cron job to execute the schedule command every minute?
https://laravel.com/docs/5.5/scheduling#introduction
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. When the schedule:run command is executed, Laravel will evaluate your scheduled tasks and runs the tasks that are due.
Your code looks fine, assuming you have cron configured correctly, this may help you debug your issue https://laravel.com/docs/5.5/scheduling#task-hooks
Still I would double check if your cron is working, and set according to https://laravel.com/docs/5.5/scheduling#introduction
In Kernel.php I have several tasks that runs once or twice a day.
When I call php artisan schedule:run in the console I can see a response with "No scheduled commands are ready to run" or "Running scheduled command: xxxx".
I want to retrieve this messages to store them while running function schedule(Schedule $schedule){} in Kernel.php
The last think I tried is using ob_start(); and ob_get_contents(); but theese only returns my own echo();.
Adding ->getSummaryForDisplay() to the register command line doesn't display if a command was executed or not.
You can get the output as described here:
$schedule->command('emails:send')
->daily()
->sendOutputTo($filePath);
to save the output to a file. Or:
$schedule->command('emails:send')
->daily()
->appendOutputTo($filePath);
to append it to a file.
You can also get the output by email but I believe you'll still have to use a file as well.
I added this to the framework: 'liebig/cron'.
I added that code to the routes.php:
Route::get('/cron/run/c68pd2s4e363221a3064e8807da20s1sf', function () {
Cron::add('example1', '* * * * *', function() {
$u = User::find(5);
$u->fullname = time();
$u->save();
});
$report = Cron::run();
print_r ($report);
});
That page will be: http://localhost:8000/cron/run/c68pd2s4e363221a3064e8807da20s1sf.
I tried to make cron jobs by the Task Scheduler, I did it to 'c:/wamp/bin/php/php5.4.12/php.exe' with argument 'localhost:8000/cron/run/c68pd2s4e363221a3064e8807da20s1sf' but it didn't worked.
The only way it working it using chrome:
'C:\Users\*****\AppData\Local\Google\Chrome\Application\chrome.exe' with argument 'http://localhost:8000/cron/run/c68pd2s4e363221a3064e8807da20s1sf'
But I don't want it, I want it would be running by php and without opening every minute the chrome.
Any ideas to make that page will be running by php.exe ?
I hope you understood the question. Thanks.
Create batch file with these two lines
cd c:\laravel-project\
c:\php5\php.exe artisan schedule:run 1>> NUL 2>&1
Go to Windows 10 Task Scheduler
Click Create basic task, choose When I logon trigger and then choose Start a program -> your .bat file.
Check Open properties dialog option and click Finish.
In task properties click Triggers, then click New and add new trigger Repeat task every 1 minute
That's because you're using the wrong tool. Do you have msysgit installed? If you have you could just do a:
curl -i http://localhost:8000/cron/run/c68pd2s4e363221a3064e8807da20s1sf
Alternatively, try the ping command:
ping http://localhost:8000/cron/run/c68pd2s4e363221a3064e8807da20s1sf
I have cPanel on a shared server and I need to run a shell script everyday.
This script has to call a controller function that scraps a webpage everyday.
My problem is that I don't know how to do a shell script that calls this function and add it at cron jobs in cpanel. How do I do all of this?
Thanks in advance!
I'm going to extend my question.
I have this code "TestTask.php":
class TestTask extends Shell {
function main() {
$this->out('Hello world.');
}
function execute() {
$this->out('Hello world 2.');
}
}
It's located in: /home/myuser/public_html/app/Console/Command/Task
And then, my cronjob is like this:
* * * * * php /home/myuser/public_html/app/Console/cake.php test -app /home/myuser/public_html/app >> /home/myuser/public_html/file.log
The last part it's to log the output in a file.
It doesn't work at all.
Help please!!!
Here's the command for a cron job I am using on my server:
/home/path-to-cake/lib/Cake/Console/cake -app /home/path-to-cake/app time
where time is my shell, located in /app/Console/Command/TimeShell.php. This is how you run shells.
Now, on the other part of your question, a task must be called from within a shell. From the code you posted, it seems that you are confusing a bit shells with tasks. A shell can contain tasks, just like controllers can have components. main(); is required for a shell when it is called without any arguments. It is not necessary for a task to implement it. On the other hand, a task must implement an execute(); method.
In order to run a task from your shell, be sure to add public $tasks = array('Test'); in the TestShell class; You can use this task in your shell just like you use a component in a controller: $this->Test->whatever();
For more info on CakePHP 2.0 shells and tasks, have a look here
Hope this helped!