Web Star
Web Star

Reputation: 432

Larave 8 Cronjob not working with Hostinger shared service

I have created a schedule to send emails after a given time and this works perfectly locally. My code is very standard and I don't think there's anything wrong with that.

This is because locally it works perfectly with the php artisan schedule:run command.

Since the cronjob input field in the Hosginger service does not allow special characters, I had to create a bash file.

This is my bash file(path: app/Console/cron.sh).

#!/bin/sh
php /home/uidnumber/domains/domain_name/public_html/laraveldirectory/artisan schedule:run 1>> /dev/null 2>&1

And this sh file was registered in cronjob.

However, the expected behavior does not proceed.

To make sure the bash file is working, I tried the following to confirm the bash file is called correctly.

I created a script.php file in the root directory and changed cron.sh file like following.

#!/bin/sh
php /home/uidnumber/domains/domain_name/public_html/laraveldirectory/script.php cron:run > /dev/null 2>&1

For reference, in script.php, I wrote a script that puts dummy data in a specific table in the DB for testing. This worked perfectly.

In summary, my thoughts are:

  1. I think there is no problem with the schedule code because it works perfectly on local with the command php artisan schedule:run.
  2. The bash file call was also confirmed through the execution of the script.php file.

So.. I can guess that something is wrong with the following command in the bash file, but I couldn't succeed despite trying various ways.

#!/bin/sh
php /home/uidnumber/domains/domain_name/public_html/laraveldirectory/artisan schedule:run 1>> /dev/null 2>&1

Can anyone help me what I have to fix?

Upvotes: 0

Views: 1294

Answers (2)

FelipeC
FelipeC

Reputation: 9508

Cron jobs do not generally have the same environment, so you have to specify the full path to php (e.g. /bin/php). A lot of times these services have multiple php versions, so the correct one might be under a different PATH.

What I would do is create a script that outputs all the relevant information and then take a look at what's different from an environment that works:

#!/bin/sh
env >> /tmp/env.txt
command -v php >> /tmp/php.txt

Generally when debugging these kinds of problems you have get rid of all the assumptions, including that php is available.

Upvotes: 1

Jason Kurzik
Jason Kurzik

Reputation: 101

I'd guess your testing user has important variables set in .profile, .bashrc, or something like that which allows the php artisan schedule:run command to run, in or out of a script. I've encountered similar problems in the past with Java programs.

Cron does not launch a shell, and thus will not obtain any variables set outside of the script. Make sure any environment variables you need to run the script are present within the script.

This includes information about where the script is located. If running a command relies on an environment file in the current working directory, it will not work in a cronjob unless that directory is changed into during the execution of the script.

Upvotes: 0

Related Questions