Crontab isn't running Laravel scheduled jobs - php

I've set up crontab on my AWS-EC2 instance to hit the Laravel scheduling endpoint every minute via the root account using sudo crontab-e:
* * * * * php ~/htdocs/artisan schedule:run >> /dev/null 2>&1
However, despite to the cron logs showing it is indeed running every minute:
Jan 26 12:02:01 ip-172-31-28-116 CRON[5057]: (root) CMD (php ~/htdocs/artisan schedule:run >> /dev/null 2>&1)
the job itself isn't executing.
Running the command php ~/htdocs/artisan schedule:run >> /dev/null 2>&1 straight up triggers the job and works.
I'm really struggling with what is going wrong here, am I missing something?

So, I failed to heed the cron output "No MTA installed, discarding output" - Upon installing an MTA (postfix, via sudo apt-get install postfix), it turned out that for the cronjob, php wasn't findable.
Changing the command to use the output of which php to:
/opt/bitnami/php/bin/php /home/bitnami/htdocs/artisan schedule:run
is now working.
Thanks for your help!

Use absolute paths when adding cron entries. ~/htdocs/artisan that should be set using the full path to your application root directory.
It works when you manually run the command because your environment is set accordingly. Not the case when adding cron entries using sudo.

Related

Linux scheduled task not working in Laravel

The below cronjob is not working, although the task itself is working when I manually run it using php artisan q:calc .
I just added the path for php and artisan files as shown below, and pasted the command in the terminal.
Am I missing something ?
* * * * * /usr/bin/php /var/www/html/sharp/artisan schedule:run >> /dev/null 2>&1
That command is a cron entry, not something you run in terminal.
For example, under the specific user you would run (depending on your environment):
$ crontab -e
And paste the above to the crontab file.
You can learn more in the docs:
https://laravel.com/docs/master/scheduling
Or by researching how to add cron entries for your specific operating system.

Laravel cronjob schedule command not firing on production

I have 7 different schedule commands on kernel.php and one of those is not firing when running the schedule.
kernel.php
....
$schedule->command('my:command')->hourlyAt(15); // this wont run
...
Running php artisan my:command manually on command line works fine.
Also when running the scheduler on our dev server, all commands works fine. The problem is only on production server.
There are no errors on log files.
Any ideas what might be wrong?
I'm using Laravel 5.6
UPDATED:
The problem was wrong artisan path on laravel forge scheduler
Have you add following cron entry as per your project folder path ?
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
please check with following commands
crontab -l
If not ?
open crontab by
crontab -e
add * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 entry at the end of file
save file and run following commands
sudo service cron restart
again check with crontab -l
this command will return already set cronjob
i hope it helps :)

Running codeigniter 3 cron job issue

I'm trying to run a codeigniter 3 cron job. If I open the file manually it works through the browser and I find databse updated and emails are sent
https://www.example.com/module_name/controller/method
But not working through a cron job like this every minute on a private server
curl --silent https://www.example.com/module_name/controller/method
Also tried
/usr/local/bin/php /home/username/public_html/index.php module_name controller method
Any idea or other ways to run it?
I'd first go to a terminal and check that your php is actually at /usr/local/bin/php by running:
which php
You mentioned that you'd like to know other ways to run the cron, and I've used wget many times. For you that would look something like:
/usr/bin/wget https://www.example.com/module_name/controller/method -O /dev/null
In most(all?) Linux distros, you're going to open crontab for editing using:
crontab -e
Once in there, just add a line:
* * * * * /usr/bin/wget https://www.example.com/module_name/controller/method -O /dev/null
Do ensure that you have wget available, and it's location by running:
which wget

Laravel Schedular not working automatically in server (CPanel)

I have this in my Kernal.php
$schedule->call(function () {
DB::table('news')->delete();
})->everyMinute();
when i do
php artisan schedule:run
it works fine.
But when i use cpanel and write in cron job
php /home/allnewsnepal/public_html/artisan schedule:run >> /dev/null 2>&1
the code doesnt run automatically.I dont have access to shell of cpanel.
For cron's in cPanel, you can look in this post:
Run a PHP file in a cron job using CPanel
The things that you should pay attention to are:
Global path of your PHP (e.g. /usr/bin/php)
Global path of your Laravel (e.g. /var/www/html/LaravelProjectName)
In order to start a cron job on Linux based systems, you must specify the user for that cron, let's say the user is root, so the cron job would look like this:
root php /home/allnewsnepal/public_html/artisan schedule:run >> /dev/null 2>&1
Of course with * prefixes depending on your cron schedule
Thanx for the help .
I got my problem solved by doing
php-cli -q /home/allnewsnepal/public_html/artisan schedule:run

Laravel scheduler not working

I have a Laravel 5 application where i want to run cronjobs, i have created a command, that will be running every 5 minutes.
But for some reason it´s never called.
I have added the following crontab.
* * * * * php /var/www/vhosts/website.com/httpdocs/artisan schedule:run
I checked the cron log file, and it looks like it´s running:
Apr 15 10:19:01 lvps92-51-xx-xx CROND[15420]: (root) CMD (php /var/www/vhosts/website.com/httpdocs/artisan schedule:run)
But it is never calling the command.
The funny thing is that if i run the command manually its working...
[root#lvps92-51-xx-xx /]# php /var/www/vhosts/website.com/httpdocs/artisan schedule:run
Running scheduled command: (touch /var/www/vhosts/website.com/httpdocs/storage/framework/schedule-c56ad4a76ba9d8e31def649e20c42f73; /usr/local/php566-cgi/bin/php artisan test:run;
rm /var/www/vhosts/website.com/httpdocs/storage/framework/schedule-c56ad4a76ba9d8e31def649e20c42f73) > /dev/null 2>&1 &
There is no logging entries in the Laravel Log.
(I know that i run the cronjob as root, but that was to avoid permissions errors doing testing)
Laravel version: 5.0.27Server: Centos 6
What am i missing?
I also had this problem few days ago and this is how I solved it.
I am actually using hostmonster for hosting my application.
My cron job runs every minutes.
* * * * * /usr/local/bin/php path/to/artisan schedule:run 1>> /dev/null 2>&1
Hope this helps
Found out what was wrong, i have more than 1 PHP version installed and for some reason cron is using the default php installation even when i have added the new PHP path to .bash_profile.
I fixed it by adding path to the right PHP version:
* * * * * /usr/local/php566-cgi/bin/php /var/www/vhosts/website.com/httpdocs/artisan schedule:run 1>> /dev/null 2>&1
That way its forced to run with that PHP version.

Categories