Search code examples
phplinuxsymfonycronbeanstalkd

Crontab do not run the php command


I am using Symfony 3 framework with pheanstalk php library. I run the app on server with Linux Debian Jesse. The job creation works ok and if run the worker from terminal it works like it should. But when I added the command to crontab I see that the command do not work. There is not any log in /var/main/user to help me with debuging. I will be very glad for any help.

This is my worker command (only the execute function):

protected function execute(InputInterface $input, OutputInterface $output)
{
    $output->writeln("\n<info>Beanstalk worker service started</info>");
    $tubes = $this->getContainer()->get('app.job_manager.power_plant')->getTubes();
    if ($input->getOption('one-check')) {
        // run once
        foreach ($tubes as $tubeName) {
            if ($tubeName != "default") {
                $this->getContainer()->get('app.queue_manager')->fetchQueue($tubeName);
            }
        }
        $output->writeln("\n<info>Beanstalk worker completed check and stoped</info>");
    } else {
        // run forever
        set_time_limit(0);
        max_execution_time(0);
        while (1) {
            foreach ($tubes as $tubeName) {
                if ($tubeName != "default") {
                    $this->getContainer()->get('app.queue_manager')->fetchQueue($tubeName);
                }
            }
        }
        $output->writeln("\n<info>Beanstalk worker stoped</info>");
    }
}

This is my app.queue_manager function to get job from queue and run command:

public function fetchQueue($tubeName)
{
    if ($this->pheanstalk->getConnection()->isServiceListening()) {
        while (true === is_object($job = $this->pheanstalk->watch($tubeName)->ignore('default')->reserve(self::WATCH_TIMEOUT))) {
            $data = json_decode($job->getData(), true);
            $this->worker($data);
            $this->pheanstalk->delete($job);
        }
    }
}

And the worker to run command

public function worker($data)
{
    $application = new Application($this->kernel);
    $application->setAutoExit(false);

    $parameters = [];
    $parameters['command'] = $data['command'];
    foreach ($data['meta'] as $key => $param) {
        $parameters[$key] = $param;
    }

    $input = new ArrayInput($parameters);
    $output = new NullOutput();

    return $application->run($input, $output);
}

This is my crontab that do not work:

@reboot /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start

I created another cron tab that works ok. It works every 15 min, and the difference is that do not have infinite loop (while(1)) so it goes only once thru tubes and than finished. But it is not what i want. I want infinite loop that works all the time like I created it with first crontab:

*/15 * * * * /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start --one-check

Solution

  • I used rc.local. It solve the problem. Tnx FreudianSlip