cocur/background-process

Run succeed but not found in background process list

giselajesslim opened this issue · 18 comments

I have a code which is collecting data.
When I run your code, I got the PID but I dont get the data.
So, I check the background process with terminal and I found nothing with the same PID.
When I run manually with "php collect.php" command, my code run normally and I get the data.
Is there anything I miss?
Thanks.

This is my code.

$process = new BackgroundProcess("php /var/www/html/project_name/public/assets/collect.php");
$process->run();

echo sprintf('Crunching numbers in process %d', $process->getPid());
while ($process->isRunning()) {
        echo '.';
        sleep(1);
}
echo "\nDone.\n";

What if you set full path to php, like /usr/bin/php?

$process = new BackgroundProcess("/usr/bin/php /var/www/html/project_name/public/assets/collect.php");

You can find out where php is running whereis php

I tried to put full path php too. (/usr/bin/php or /usr/share/php)
But still doesnt work and only return PID as output.
shell_exec() enabled in php.ini too.

Use that PID and run in console using that PID.

Let's say PID is 3270

ps aux |grep 3270

What is the output?

PID is 11140.
This is the output.

root     11222  0.0  0.0  11740   936 pts/1    S+   04:46   0:00 grep --color=auto 11140

It's not running then at all. Well, let's see what the dev is going to say. It's hard to tell without being at the PC.

S means interruptible sleep (waiting for an event to complete). It should have to be R right?

Yes,

PROCESS STATE CODES

       D    Uninterruptible sleep (usually IO)
       R    Running or runnable (on run queue)
       S    Interruptible sleep (waiting for an event to complete)
       T    Stopped, either by a job control signal or because it is being
            traced.
       W    paging (not valid since the 2.6.xx kernel)
       X    dead (should never be seen)
       Z    Defunct ("zombie") process, terminated but not reaped by its
            parent.

       For BSD formats and when the stat keyword is used, additional
       characters may be displayed:
       <    high-priority (not nice to other users)
       N    low-priority (nice to other users)
       L    has pages locked into memory (for real-time and custom IO)
       s    is a session leader
       l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
       +    is in the foreground process group

That output is simply piped grep command.

To avoid that output and have the output being empty, in case there is no processes found, you could try ps aux | grep -v grep | grep nameOfProcess

what is the

nameOfProcess

command? or PID?

You can use any PID/NameOfProgram

Are you sure that the collect.php is still running when you execute ps aus | grep PID?

What do you mean by "I got the PID but I dont get the data"? BackgroundProcess pipes on purpose all output of the command to /dev/null (that is, it ignores the output) because BP should continue to run even if the parent process (the process that runs the BP code) finishes.

If you want to process the output of the command you either need to use some kind of IPC (inter-process communication) or something like Symfony\Process.

What I mean by "I got the PID but I dont get the data". I run the code in the documentation and I get the PID. Does PID mean the code is succeed?
FYI, My collect.php is collecting data from twitter. it will be created json files.
in this problem, the code does not create json files, which means it does not run.

Ah, ok. When you try console, you're running your code as root. When you do it using PHP, you don't run it as root most likely but like virtual server owner. At first, try deleting that file which is created after running collect.php. I think the file can't be written in this case, as previously was created by root user.

If you get the PID it means that the process started to run. You an pass an output file as argument to run() that stores the output of your command.

$process->run('log.txt');

Check the content of log.txtand see if an PHP error occurred.

@qooob : there is no files created after running collect.php. So, I assume the process is not running. I changed folder permission to 0777.

@florianeckerstorfer : yes there is an error in my files. thanks. I need to fix it first and try again.

PHP Warning:  require_once(../../lib/fennb/phirehose/lib/Phirehose.php): failed to open stream: No such file or directory in /var/www/html/project_name/public/assets/collect.php on line 2
PHP Fatal error:  require_once(): Failed opening required '../../lib/fennb/phirehose/lib/Phirehose.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/project_name/public/assets/collect.php on line 2

Its running now after I can see what is the problem with PHP error.
Thanks.