Home > OS >  Unable to run artisan command via cron in localhost [Laravel 8] [Solved]
Unable to run artisan command via cron in localhost [Laravel 8] [Solved]

Time:04-18

I'm new with both laravel and crontab. I have a laravel 8 project and want to use cron to run the scheduled task. But i keep getting error. The task that i put in the cron tab is like this,

* * * * * /usr/local/bin/php /home/mydirectory/path/to/project/artisan schedule:run >> /tmp/laravel.log

But, when it is executed, the log give the following error.

Could not open input file: /home/mydirectory/path/to/project/artisan

I tried running the task in terminal/command line, and it is executed just fine. The weird thing is that i was able to run cron job task for my previous laravel project (the cron job task has been deleted though), but not for this project. I have use chmod 755 as well.

I'm really confused what is wrong with this one :( Btw i'm using macos and laravel 8.83. Thanks for your help!

CodePudding user response:

Based on the Laravel documentation you need to first change the current directory to project root then use the command you want to run with artisan.

https://laravel.com/docs/9.x/scheduling#running-the-scheduler

* * * * * cd /home/mydirectory/path/to/project && php artisan schedule:run >> /dev/null 2>&1

CodePudding user response:

Turns out i haven't give full disk access to cron :)

For mac users, you might try to give full disk access to cron so that it will be able to access users folder. To do so, go to Security and Privacy in System Preferences > find Full Disk Access in Privacy tab > drag "cron" file from "/usr/sbin/cron" in directory to Full Disk Access

Ref: https://osxdaily.com/2020/04/27/fix-cron-permissions-macos-full-disk-access/

  • Related