Welcome Guest, Not a member yet? Register   Sign In
problem with shell_exec in a library.
#1

[eluser]oll[/eluser]
For the youtube-like website I'm developping for my work, I use the function below, with ffmpeg as Command, to convert a video file into flash, since it can take a long time to be converted (I redirect the output and then "ajax" it in a webpage, so that the user can go on doing something else meanwhile).
background process with php

Code:
function run_in_background($Command)
   {
           $PID = shell_exec("nohup $Command 2> /dev/null & echo $!");
   }

It works well with a simple PHP page.
It also works well with codeigniter if I put this function directly in the controller and call it with another function (for instance, index() )

Code:
$pid = $this->run_in_background($videofile);

It works means "I have the pid instantantly" and the web site goes on, while ffmpeg process in the background.

But I would like to use it with the library I created and it doesn't work.
It's defined as a method in my library (a simple copy/paste from this function in the library file)

I have another method, createflash(), in which there's a simple :

Code:
function createflash() {
//...
$this->pid = $this->run_in_background($this->filename);
//...
}

There's no error, but it waits for the function to finish (it can take several minutes) before finally displaying me the result . As if there wasn't the "nohup".

It looks like an instantiation behaviour problem, but I can't find where, since I'm far from being a specialist Wink

Thx.
#2

[eluser]antonumia[/eluser]
If by 'library' you mean a model you need to include it in your controller constructer:

function MYClassname(){
$this->load->model->modelname;
}

then in your function:

$PID = $this->modelname->functionname();


Also if you're doing flv encoding using ffmpeg you should also install flvtool2 to insert the metadata after the flv has been created e.g.

shell_exec("flvtool2 -UP ".$filepath );
#3

[eluser]oll[/eluser]
Thank you for the answer (and for flvtool2 tip !).
Yes, actually, I spoke about a library ( I created an extension from the php_ffmpeg one).

But anyway, it finally apperas to be a shell_exec problem when using the redirection in the script (It's a real mess to make that work), so unrelated to CI, sorry.

The test I did :


Code:
<?php

class Testbg extends Controller {

    function Testbg()     {
        parent::Controller();
        
        $this->load->library('testlib');
        //$this->load->model('testmod');
        
    }
    
        function testfgcontroller () {
        $this->benchmark->mark('code_start');
        
        exec("sleep 5 >/tmp/log 2>&1 ");
        
        $this->benchmark->mark('code_end');
        echo $this->benchmark->elapsed_time('code_start', 'code_end');
    }
    
    function testbgcontroller () {
        $this->benchmark->mark('code_start');
        
        exec("nohup sleep 5 >/tmp/log 2>&1 &");
        
        $this->benchmark->mark('code_end');
        echo $this->benchmark->elapsed_time('code_start', 'code_end');
    }
    
    function testfglib () {
        $this->benchmark->mark('code_start');
        
        $this->testlib->fg();
        
        $this->benchmark->mark('code_end');
        echo $this->benchmark->elapsed_time('code_start', 'code_end');
    }
    
    function testbglib () {
        $this->benchmark->mark('code_start');
        
        $this->testlib->bg();
        
        $this->benchmark->mark('code_end');
        echo $this->benchmark->elapsed_time('code_start', 'code_end');
    }
    
}
?>

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Testlib {
    
    function Testlib() {
    }
    
    function fg() {
        exec("sleep 5 >/tmp/log 2>&1");
    }
    
    function bg() {
        exec("nohup sleep 5 >/tmp/log 2>&1 &");
    }
}
?>

The results :

http://videos/index.php/testbg/testfgcontroller : 5.0071
http://videos/index.php/testbg/testbgcontroller : 0.0063
http://videos/index.php/testbg/testbglib: 5.0068
http://videos/index.php/testbg/testbglib : 0.0065

There's so absolutly no problem with CI Wink




Theme © iAndrew 2016 - Forum software by © MyBB