Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dreferr's avatar

How to perform `php artisan schedule:run` on an Ubuntu server with Laravel 5.3 for Cron Jobs

Hello, I'm running a Digital Ocean's 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 needed
* * * * * 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
* * * * * /path/to/php /path/to/artisan scheduled:run 1>> file.log

I've tried creating a .sh file with a php artisan schedule:run command inside it and seeing it the cron can call that .sh file once every minute to get the scheduler running but it doesn't work.

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!!

0 likes
6 replies
ejdelmonico's avatar

I am not sure how you would go about doing this but I know it can be done because I use Laravel Forge to schedule cron jobs and it works just fine. So, you most likely do not need any plugins to get the job done.

From past experience though, I believe you need to use /dev/null in the cron instructions. Have a look around https://serversforhackers.com for more info.

1 like
Dreferr's avatar

Thank you for the quick response! I'll have a look at the resource you posted, thank you. I'm trying using the dev/null you've suggested and it's not working, I've even attempted it in multiple different variations.

SHELL=/bin/bash
HOME=/
* * * * * ./var/www/cron-task.sh >> /dev/null 2>&1
* * * * * var/www/ ./cron-task.sh >> /dev/null 2>&1
* * * * * var/www/./cron-task.sh >> /dev/null 2>&1
* * * * * /usr/bin/php /var/www/artisan scheduled:run >> /dev/null 2&1

I've even tried it using a .sh file to see if I can call the command php artisan schedule:run itself by the cron job calling the .sh file. However, that fails too. Do you happen to know where the error logs may be stored for crontab on Digital Ocean?

fideloper's avatar
Level 11

Here's some questions / things to check:

First - What does "it's not working" mean? What's your evidence (how are you checking?)

In general:

  1. crontab -e will run PHP as the user you're logged in as when making the cron job. If that user doesn't have permission to do things the PHP task may try to do (write to file system perhaps). you'll run into issues
    • If PHP is normally run as user www-data during a web request, you may also want to run crontasks like that - sudo crontab -u www-data-e
  2. Don't use relative file paths for application code. Perhaps try * * * * * cd /var/www/app && php artisan schedule:run
  3. Check your laravel app logs for errors. The laravel log is the best place to check that out. (e.g. /var/www/app/storage/logs/laravel.log)
  4. Check your syslog to ensure the crontab is running (/var/log/syslog). You should see a line in there for every call to a cronjob throughout the system
  5. Make sure your .env file is set correctly

One thing to try is to run that command manually and see if you get any output:

# While logged, cd to whereever your app is
cd /path/to/app

# Run the command as your current user
php artisan schedule:run

# See if results in 0 (success) or non-zero (failure or some sort). This following
# will echo out the return statement of that command
echo $?

# perhaps it's failing silently?

In other words, find out if there's an error in your APP first. Then check the laravel log file, and the syslog for errors.

5 likes
Dreferr's avatar

You're right @fideloper, I should've listed what my logs were showing. I was, however only checking the syslog but the laravel.log had some more valuable information that could've helped in resolving this issue days ago. I wasn't checking it because I thought that the application wasn't even being touched by the server's cron.

After a lot of digging 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

There were a few things I could've did better to more effectively resolve the issue but regardless, I appreciate all the help!

1 like
fideloper's avatar

Ah ha!

To clarify - this isn't necessarily a read-permission issue - the root user can read any file.

The change chmod +x worked as it made the file executable (for user, group and "other" - anyone else), so running that .sh file like ./cron-task.sh will "just work" (it'll execute bash based on the presence of #!/bin/bash being at the top of the .sh file).

Alternatively, you could have used bash cron-tash.sh to get that to run (I usually go the chmod +x route)

Glad the idea to check out the laravel logs lead to a fix!

1 like
pkundariya's avatar

for me it is not working,

protected function schedule(Schedule $schedule) { // code here }

this above function is calling every second but not executing command.

my command style is below $schedule->command('commandname')->everyMinute();

1 like

Please or to participate in this conversation.