Laravel 5 How to run a php file via scheduler - php

First time ever setting up a cron/scheduler. How do I run a php file with the scheduler? This is what I see in Laravel's documentation...
Entered the following command via Putty/SSH...
php /path/to/artisan schedule:run >> /dev/null 2>&1
Now... In the Kernel.php file... do I simply add the path to the php file that I want to run in the statement below?
$schedule->exec('node /home/forge/script.js')->daily();

After a few days... I have it working.
I set up the cron job in my cpanel. I set it up to run every minute so I could get good feedback on what I was doing wrong. I created a text file to record the errors.
php /home/accountname/artisan schedule:run >> /home/accountname/cron-output.txt 2>&1
I thought I had to call the php file where the method was located. However, to call a method, you must put the full path to the method like so...
$schedule->call('App\Http\Controllers\ParseDataFeed#parseFeed')
->dailyAt('15:00')
->sendOutputTo('cron-output.txt');
I hope this helps someone.

Related

Creating Cron Job on cPanel for Task Scheduling in Laravel

Pretty much the title. I am doing it as it says in the docs but it won't work. No log updates in laravel.log file either which might have helped. This is how I set up my cron job:
I also gave it my mail address where it should send the log of the job and it sometimes does do that but sometimes doesn't which I didn't quite understand why. Anyway, the mail content was simply what Laravel says when you type php artisan in the terminal. Like it shows you all the commands of Laravel.
Any idea why it doesn't work and how to fix it? Thanks!
You should to change the line of command to this:
/usr/local/bin/php /home/cpanel-user/laravel-app/artisan schedule:run >> /dev/null 2>&1

Laravel cron on Digitalocean

I have set up Laravel scheduler to run my custom commands at specific time. Now I wanted to set up cron on Digitalocean server to trigger schedule:run each minute to check if something is scheduled at the given time.
After initial SSH-ing to server, I have run crontab -e and added the following line to it:
* * * * * php /var/www/Laravel artisan schedule:run >> laravel_cron.log
but the problem I'm facing is that I don't see anything written in laravel_cron.log, but it does get created, so now I have no idea whether my commands will actually be ran.
To test it out, I have tried entering php /var/www/Laravel artisan but I get no output in command line.
If I change the route to say xyz/www/Laravel it is saying that it can't find it, so I guess the route is set up fine. Also when I manually go to the Laravel folder and run php artisan without the route in the middle, I get the standard output.
I believe the command php /path/ artisan schedule:run does not return any output.
If you want to log the output of a task, you can use sendOutputTo or emailOutputTo
e.g.
$schedule->command('foo')
->daily()
->sendOutputTo($filePath)
->emailOutputTo('foo#example.com');
More examples can be found here

Php Cron job not working, using crontab -e command

i want to set a cron job on a server but its not working. I know there are hundreds of links on web that shows how to setup a cron job but i cant seem to make it work. What im doing now is:
1) Running crontab -e.
Then it shows bunch of lines in the command line.
2) I go to the bottom and add */5 * * * * path/to/myfile.php
and then i exit the editor in command line. Please tell me whats wrong here. Do i need to put my file in a specific folder? or do i need to go to the desired folder and then use crontab -e, or something else. Please forgive me, this is my first cronjob, hoping to be better next time.
Here are the pictures of what im doing.
Did you restart the cron service after you updated the file?
Have you tried executing the php script from the command line first to verify that it's executing as expected? It might be that the cron task is executing but the script is failing. If the script is fine, you might want to try using php as a command followed by the path and filename of the php file and then quitting the execution after it's done with -q.
*/5 * * * * php path/to/myfile.php -q
The problem could well be that you are trying to execute a PHP file and your system is unaware of what to do with it.
Is your PHP file executable?
You can make it executable by running
$ chmod +x file.php
and if you add a shebang to it
#!/usr/bin/php
<?php
// ...
the PHP script can be executed by running
$ ./file.php
Alternatively, you need to run the PHP interpreter and pass it the path to the file as an argument.
$ php file.php
For reference, see:
http://php.net/manual/en/features.commandline.usage.php

Laravel everyTenMinutes not working

In my Kernel.php file I had :
$schedule->command('tokens:manage')->everyTenMinutes()->withoutOverlapping();
It did work till 4 days before and now it is not working anymore. I've not made any changes to the Kernel except adding two commands to run.
So I thought it was a memory problem, and modified my TokensManage.php command in order to optimize it. But the problem was still there.
I've modified Kernel.php into :
$schedule->command('tokens:manage')->withoutOverlapping();
or into :
$schedule->command('tokens:manage')->everyFiveMinutes()->withoutOverlapping();
And it's working, any ideas ?
withoutOverlapping() creates a file in storage/framework (schedule-*). If the command does not end gracefully, that file will not be deleted, and to the program it will seem that the command is still running.
https://laracasts.com/discuss/channels/laravel/laravel-task-scheduler-after-fatal-error-when-withoutoverlapping-is-set
You need start the cron.
Here is the only Cron entry you need to add to your server:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. Then, >Laravel evaluates your scheduled tasks and runs the tasks that are due.
See here and here.

How to create a cron job in cpanel to run a PHP file every hour?

First time I am creating a cron job in cpPanel to run a PHP file every hour.
My cron command is:
/usr/local/bin /home/simplemediaplaye/public_html/new.php
What things am I missing here? This file runs well manually, but not by a cron job. Can you suggest what I need to do to run my cron successfully?
Hello If I am not mistaking, you're missing the "-q" command and PHP ? see below:
/usr/bin/php -q /home/simplemediaplaye/public_html/new.php
Let me know if that helps.

Categories