I’m running a Laravel application on AWS Elastic Beanstalk and have set up a cron job to execute php artisan schedule:run
using the following cron configuration file:
files:
"/etc/cron.d/vehicles-update-status":
mode: "000644"
owner: root
group: root
content: |
SHELL=/bin/bash
* * * * * root . /opt/elasticbeanstalk/deployment/env && cd /var/www/html && /usr/bin/php php artisan schedule:run >> /var/www/html/laralog.log 2>&1
commands:
01_restart_cron:
command: "systemctl restart crond"
test: "systemctl is-active --quiet crond"
However, when the cron job runs, it fails to connect to the database and I receive the following error:
LSTATE[08006] [7] connection to server at ‘127.0.0.1’, port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections? (Connection: pgsql, SQL: select * from “vehicles” where “vehicles”.“deleted_at” is null)”
The database works perfectly when accessed from the Laravel application itself. I’ve tried various configurations, but the cron job consistently fails with the same database connection error.
-
I’m using Laravel 11 and PostgreSQL as the database.
-
The cron job is running on an EC2 instance in AWS Elastic Beanstalk.
-
The Laravel application’s
.env
file contains the correct database credentials. -
I’ve verified that the
crond
service is running.
What I’ve tried:
- I’ve tried various settings in the configuration file
JoãoM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.