Cron Jobs with Laravel on Ubuntu 16.10 x64

December 25, 2016 4.1k views
Apache DigitalOcean PHP PHP Frameworks Ubuntu

Hello,
I'm running an Ubuntu 16.10 x64 and have deployed the php Framework Laravel 5.3.28 on the server.
For the most part everything is working as normal however I'm trying to get the crontab to call artisan commands like php artisan schedule:run so that laravel's task scheduler can be put to use.

I do not wish to download any extra packages to make this work as I feel there shouldn't really be a need to since the cron can call, what looks like, any command if properly coded.

What I'm trying to do:
From within the crontab -e I'm trying to properly write the Ubuntu correct way to call php artisan schedule:run

What I've done:
I've currently tried multiple different ways of writing this command and nothing is working as noted below:

#Attempt for cron
SHELL=/bin/bash
#PATH=???? <--- Confused if this is even necessary
* * * * * php /path/to/artisan scheduled:run 1>> file.log 2>&1
* * * * * /path/to/ php artisan scheduled:run 1>> file.log 2>&1
* * * * * /path/to/ php artisan scheduled:run >> file.log
* * * * * /path/to/ php artisan scheduled:run 1>> file.log
* * * * * /path/to php artisan schedule:run
* * * * * /usr/bin/php /var/www/artisan scheduled:run 1>> file.log

I've gone to multiple resources to try and find the answer but nothing seems to be the answer to my specific problem.

Resources:

Conclusion:
At this point I'm stuck beyond stuck. Can someone please help me? All I'm trying to do is call the php artisan command using Ubuntu's cron and I don't know the exact way to do it. ANY and all assistance is greatly appreciated.

Thanks in advance,

Happy Holidays!!

2 Answers

Thank you @ryanpq for the answer you provided here

After a lot of digging and logging I found that the root user apparently did not have permission to access the .sh file that I made as an alternative for crons to directly access the php artisan commands.

I had to install a mail server called Postfix to be able to dump the cron's more detailed error logs into this mail server in order to see what was REALLY going on behind the scenes on the server side.

sudo apt-get update

sudo apt install mailutils

I chose all the default settings instead of trying to set it up and had it dump the routed server notifications into a /var/mail/root.log directory to which it then showed me all the errors that were occuring.

From there I found that one of the many bugs was that the root user didn't have permission to read the .sh file which was fixed with the below example:

(*Note - Be sure to cd into the directory where the .sh file lives)

chmod +x cron-task-here.sh

Then, through a lot of trial and error, I found that you had to access the artisan with your crontab's HOME config set to:

HOME=/

and then have the .sh read as follows:

#!/bin/bash

php /path/to/artisan schedule:run

Several of the crontab entries you've tried should work. My first guess leads me to ask whether you have installed the php-cli package? Without them, you cannot run php scripts from the command line, only via a web server. If you haven't already I would recommend installing php7.0-cli

sudo apt-get update
sudo apt-get install php7.0-cli
  • Strange, because when I cd into the project directory and do php artisan schedule:run (dry-run cli command that fires only once) directly the system reacts the way it should and acts on the right classes and everything.

    In addition, since we're using Laravel (v5.3.28), php7+ is a must have otherwise many of our system's functionality would fail. However, maybe php7.0-cli is different than doing sudo apt-get install php7.0-fpm on the Ubuntu server.

Have another answer? Share your knowledge.