Sletheren
Sletheren

Reputation: 2486

Laravel Cron php_network_getaddresses: getaddrinfo failed

Hello!

So I have this annoying problem with setting up a cron job for Laravel scheduling. To understand the problem, Here is the project architecture:

We want to automatize some tasks, so we created 3 commands and we gathered them inside the schedule to run them every day, here is how the kernel.php looks like:

    protected $commands = [
        'App\Console\Commands\IncompleteAccount',
        'App\Console\Commands\TirageSoon',
        'App\Console\Commands\WinzExpiration',
    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('incompleteaccount:run')->everyFiveMinutes()->sendOutputTo(storage_path('logs/cron.log'));

        $schedule->command('tiragesoon:run')->everyFiveMinutes()->sendOutputTo(storage_path('logs/cron.log'));

        $schedule->command('winzExpire:run')->everyFiveMinutes()->sendOutputTo(storage_path('logs/cron.log'));
    }

And our crontab looks like:

* * * * * php /var/www/artisan schedule:run >> /var/www/storage/logs/cron.log 2>&1

In the logs file we get this:

  [Illuminate\Database\QueryException]                                                                                                                                    
  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from `booking` where `created_at` > 2017-08-27 16:08:50)  



  [Doctrine\DBAL\Driver\PDOException]                                                             
  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known  



  [PDOException]                                                                                  
  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known  



  [PDOException]                                                                               
  PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known  


No scheduled commands are ready to run.
No scheduled commands are ready to run.
No scheduled commands are ready to run.
No scheduled commands are ready to run.

and finally the .env file looks like:

APP_ENV=prod
APP_KEY=base64:gPDQqknmG9YSOMueb/Ii0xXWQvrzOPsVFVRHVIzXXD0=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://www.ourSite.com
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=ourDB
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file

Also, I can ping to mysql from inside my container, and here is the docker compose file:

version: '2'

services:

    volumes_source:
      image: tianon/true
      volumes:
          - ${SRCDIR}:/var/www
          - ${DATADIR}/data/uploads:/var/www/public/img/uploads




    nginx:
        image: nginx
        volumes_from:
          - volumes_source
        volumes:
            - ${SRCDIR}/dockers/etc/nginx/sites-enabled:/etc/nginx/sites-enabled
            - ${SRCDIR}/dockers/etc/nginx/nginx.conf:/etc/nginx/nginx.conf
        ports:
           - "${HTTP_PORT}:80"
        links:
           - php:phpfpm

    wait:
        image: aanand/wait
        links:
           - mysql:mysql
### Workspace Utilities Container ###########################

    php:
        build: dockers/winsu
        volumes_from:
          - volumes_source
        command: php-fpm7 -F
        working_dir: /var/www
        environment:
            CONFIG_ENV: ${CONFIG_ENV}
        entrypoint: /var/www/dockers/winsu/run.sh
        volumes:
           - ${SRCDIR}/dockers/winsu/crontab:/crontab.file
        links:
           - mysql:mysql
           - wait:wait

### MySQL Container #########################################
    mysql:
        image: mysql
        volumes_from:
            - volumes_data
        environment:
            MYSQL_DATABASE: winsu
            MYSQL_USER: homestead
            MYSQL_PASSWORD: root
            MYSQL_ROOT_PASSWORD: root

### Databases Data Container ################################

    volumes_data:
        image: tianon/true
        volumes:
            - ${DATADIR}/data/mysql:/var/lib/mysql
            - ${DATADIR}/data/storage:/var/www/storage

Been facing this problem for 3 days so far! would really appreciate it if you can help! I already saw the similar questions, but nothing worked! Thanks in advance :)

Upvotes: 1

Views: 2367

Answers (2)

Tarun Lalwani
Tarun Lalwani

Reputation: 146540

Your problem seems to be related to environment variables not being picked up. When you run a command in cron two things are different

  • current working directory
  • environment variables

So change your command to

* * * * * cd /var/www && /var/www/artisan schedule:run ...

and define any additional needed variables using X=Y format prefixed before the command

* * * * * cd /var/www && X=Y Z=A /var/www/artisan schedule:run ...

Upvotes: 1

dieroste
dieroste

Reputation: 414

you can try to use artisan to prove your code.

In the php artisan commant you can put a -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

For example you can do:

php artisan incompleteaccount:run -v

And with this code if the code have error, the command returns you the information of the error with the line and all information about it.

Upvotes: 1

Related Questions