Laravel MySQL Error ONLY on Scheduled Tasks (via cron)

I have a task using the Laravel task scheduler than fires perfectly.
It fires to call a public method named Task::checkSpiders().

This hits an API (works fine) then should update local DB accordingly.

  • Building the MongoDB Docker image
  • Met “/bin/bash: no such file” when building docker image from scratch
  • Jenkins docker setup
  • Can't kill processes (originating in a docker container)
  • make test failed when compile Node.js 4.0.0 source code in docker container
  • Docker: Dockerfile vs docker-compose.yml
  • If i create a route as so…

    Route::get('/test', function () {
        $test = Task::checkSpiders();
    }
    

    It run’s perfectly.

    When i run it from CRON however it throws me this error…

    #0 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(55): PDO->__construct('mysql:host=loca...', 'forge', '', Array)
    #1 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php(24): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=loca...', Array, Array)
    #2 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(61): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
    #3 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\{closure}()
    #4 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php(920): call_user_func(Object(Closure))
    #5 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php(788): Illuminate\Database\Connection->getPdo()
    #6 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php(673): Illuminate\Database\Connection->reconnectIfMissingConnection()
    #7 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php(479): Illuminate\Database\Connection->run('update `tasks` ...', Array, Object(Closure))
    #8 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php(422): Illuminate\Database\Connection->affectingStatement('update `tasks` ...', Array)
    #9 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2072): Illuminate\Database\Connection->update('update `tasks` ...', Array)
    #10 /var/www/laravel/app/Task.php(60): Illuminate\Database\Query\Builder->update(Array)
    #11 /var/www/laravel/app/Task.php(85): App\Task::updateTask('a5b829767a6811e...', 'Finished', '2016-09-14 10:4...', '2016-09-14 11:5...')
    #12 /var/www/laravel/app/Console/Kernel.php(34): App\Task::checkSpiders()
    #13 [internal function]: App\Console\Kernel->App\Console\{closure}()
    #14 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Object(Closure), Array)
    #15 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Scheduling/CallbackEvent.php(61): Illuminate\Container\Container->call(Object(Closure), Array)
    #16 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php(61): Illuminate\Console\Scheduling\CallbackEvent->run(Object(Reflexions\DockerLaravel\DockerApplication))
    #17 [internal function]: Illuminate\Console\Scheduling\ScheduleRunCommand->fire()
    #18 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Array, Array)
    #19 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call(Array)
    #20 /var/www/laravel/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #21 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #22 /var/www/laravel/vendor/symfony/console/Application.php(794): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #23 /var/www/laravel/vendor/symfony/console/Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Console\Scheduling\ScheduleRunCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #24 /var/www/laravel/vendor/symfony/console/Application.php(117): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #25 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(107): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #26 /var/www/laravel/artisan(36): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #27 {main}  
    

    The code to insert to the DB ( I don’t think is relevant but i’ll add in anyway..)

    $tasks = DB::table('tasks')
                ->where('id', $id)
                ->whereNull('end')
                ->update([
                        'status' => $status,
                        'start' => $start,
                        'end' => $end,
                    ]);
    

    Also, just a note I am running this in Docker using Supervisord to manage CRON with Laravel.

    Any help much appreciated!

  • How to share Mac screen to Linux docker container?
  • How to benefit of the docker image cache when cloning a git repo
  • Why do there exist “-i” and “-t” options for the “docker exec” command?
  • My nodes Digital Ocean are UNREACHABLE from docker cloud
  • docker registry v2 ui with docker_auth
  • where should I put docker-compose.yml
  • One Solution collect form web for “Laravel MySQL Error ONLY on Scheduled Tasks (via cron)”

    Stupid Mistake;

    CRON is running under a different user thus the enviroment variables loading from .env aren’t available.

    I checked this by logging the ENV variables from a task run in CRON.

    Like so…

     Log::info(json_encode(get_defined_vars()));
    

    That returned [] an empty array.

    I’ve since hardcoded the variables to the config and all is working perfectly.

    Alternatively I could have set the ENV Variables in PHP (Incase they are used elsewhere) using…

    <?php
        putenv("KEY=value");
    ?>
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.