Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7592251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:51:41+00:00 2026-05-30T20:51:41+00:00

I’m trying to write a daemon process using code igniter library, and the problem

  • 0

I’m trying to write a daemon process using code igniter library, and the problem is when I fork the daemon process it can no longer access the CI instance and all the libraries which were initialized in __construct are also inaccessible plus if I create a new CI instance in the child process, I still can’t access any library and I get following errors:

ERROR – 2012-02-27 00:13:07 –> Severity: Warning –> include(application/errors/error_general.php): failed to open stream: No such file or directory /srv/http/FreeFileConvert/system/core/Exceptions.php 146
ERROR – 2012-02-27 00:13:07 –> Severity: Warning –> include(): Failed opening ‘application/errors/error_general.php’ for inclusion (include_path=’.:/usr/share/pear’) /srv/http/FreeFileConvert/system/core/Exceptions.php 146

Here’s my code:

class Conversion_workers
{
    function __construct() {
        $this->ci =& get_instance();
        $this->ci->load->library('Gearman');
    }

    private function is_locked($lock_file) {
        if(file_exists($lock_file)) {
            $lock = fopen($lock_file,"c+"); // open it for WRITING ("w")
            if (! flock($lock, LOCK_EX | LOCK_NB)) {
                flock($lock, LOCK_UN);
                return TRUE;
            }
        }
        return FALSE;
    }


    private function lock_file($lock_file, &$lock) {
        $lock = fopen($lock_file,"c+"); // open it for WRITING ("w")
        if (! flock($lock, LOCK_EX | LOCK_NB)) {
            //error_log('Unable to lock file.');
            return FALSE;
        }
        fseek($lock, 0);
        ftruncate($lock, 0);
        fwrite($lock, posix_getpid());
        fflush($lock);  
        return TRUE;
    }


    private function daemonize($lock_file, &$lock, &$parent)
    {

        // TODO: In the install check if pcntl_fork supported
        if($this->is_locked($lock_file)) {

            $parent = TRUE;
            return FALSE;
        }

        $pid = pcntl_fork();
        if($pid < 0) {
            log_message('error', 'Unable to fork process');
            return FALSE;
        }

        // If we got a good PID, then we can exit the parent process. 
        if($pid > 0) {
            log_message('info', 'Exiting parent as process forked successfully');
            $parent = TRUE;
            return FALSE;
        }

        ob_start();

        // Change the file mode mask 
        umask(0);

        // Create a new SID for the child process
        if (posix_setsid() < 0) {
            //error_log('Unable to create a new SID for child process');
            return FALSE;
        }

        // Change the current working directory 
        if(chdir("/tmp") < 0) {
            //error_log('Unable to change directory of the daemonize process');
            return FALSE;
        }

        // Lock in child process to get correct pid in lock file
        if(!$this->lock_file($lock_file, $lock)) {
            exit;
        }

        fclose(STDIN);  // Close all of the standard
        fclose(STDOUT); // file descriptors as we
        fclose(STDERR); // are running as a daemon.

        register_shutdown_function(create_function('$pars', 
            'ob_end_clean();posix_kill(posix_getpid(), SIGKILL);'), array());

        // Might be good idea to have register_shutdown_function() here if we want to 
        // check status when the daemon terminates.

        return TRUE;
    } 

   private function start_workers() {

        $lock_file  = $this->ci->config->item('lock_file');
        $parent     = FALSE; 

        if(! $this->daemonize($lock_file, $lock, $parent)) {
            if($parent)
                return;

        } else {

            // Start a worker
            $this->ci->gearman->gearman_worker();
            $this->ci->gearman->add_worker_function('some_function', 'some_function_fn'); 

            error_log('Starting worker ['.posix_getpid().']');

            while($this->ci->gearman->work());
        }

        error_log('Exiting worker');
        exit;
    }

    function add_to_queue($function_name, $params) {
        $this->start_workers();

        $this->ci->gearman->gearman_client();
        return $this->ci->gearman->do_job_background($function_name, serialize($params)); 
    }
} // END class Controller

The function “add_to_queue()” works fine but the problem is with “start_worker” which after forking just stops working, I cannot even access log helper functions, that’s why I’m using error_log() in my code.

I would be grateful if anyone could help please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:51:42+00:00Added an answer on May 30, 2026 at 8:51 pm

    Might it be that the daemonize method changes the working directory to /tmp? In which case the system is looking for the includes in .(/tmp) and /usr/share/pear, neither of which hold them?

    Perhaps http://www.php.net/manual/en/ini.core.php#ini.include-path will help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.