cron commands do not run, getting output /bin/sh: 1: php: not found

Viewed 44

I have the following inside my crontab -e

0,30 * * * * cd /usr/local/bin && php /var/www/artisan my_command > /var/www/storage/logs/cron.log 2>&1

But I get the following inside /var/www/storage/logs/cron.log.

/bin/sh: 1: php: not found

I tried running the following for reference:

# which php
/usr/local/bin/php
# whoami
root

I am running cron from inside a docker image (OS: Ubuntu) in a Laravel project.
I tried changing the path in different ways, but it still gave the same error.
There were a lot of similar questions, but I didn't manage to find my answer...

2 Answers

The current directory isn't in the PATH (and it shouldn't be).

Simply skip the useless cd command and run /usr/local/bin/php instead:

0,30 * * * * /usr/local/bin/php /var/www/artisan my_command > /var/www/storage/logs/cron.log 2>&1

Thanks to @Some programmer dude.
I would like to add some details, since I was not originally able to understand what he meant.

Basically when I was calling php cron would look for the file in every folder inside the PATH, but NOT the current directory (unless it is inside the PATH).

Sure, simply removing the cd is the best solution.
But to better explain the issue another perfectly working solution would be this one:

0,30 * * * * cd /usr/local/bin && ./php /var/www/artisan my_command

The only difference here, from the command I was originally using, is that I am specifying to use the current path with the ./.

Related