views:

891

answers:

4

I have a PHP script that uses the system() call to execute other (potentially long-running) programs (for interest: NCBI BLAST, phrap, primer3 and other programs for doing DNA sequence analysis and assembly).

I'm running under Windows XP, using the CLI version of PHP from a command prompt, or as a service. (In either case I communicate with it via a queue of tasks in a database table).

Under PHP4: when I hit Ctrl-C the script is stopped and any child process running at the time is also stopped. Under PHP5: when I hit Ctrl-C the script stops, but the child is left running.

Similarly, when running the script as a service, stopping the service when running it with PHP4 stops the child, with PHP5 the child continues to run.

I have tried writing a minimal test application, and found the same behaviour. The test PHP script just uses system() to execute a C program (that just sleeps for 30 seconds) and then waits for a key to be pressed.

I had a look at the source for PHP 4.4.9 and 5.2.6 but could see no differences in the system() code that looked like they would cause this. I also had a quick look at the startup code for the CLI application and didn't see any differences in signal handling.

Any hints on what might have caused this, or a workaround, would be appreciated.

Thanks.

+1  A: 

This issue occurs in at least PHP 5.1.2.

When a SIGINT is sent via CTRL+C or CTRL+BREAK, the handler is called. If this handler sends a SIGTERM to other children, the signals are not received.

SIGINT can be sent via posix_kill() and it work exactly as expected-- This only applies when initiated via a hard break.

From: http://php.oregonstate.edu/manual/en/function.pcntl-signal.php

The document has sample code for trapping CTRL+C and sending posix_kill to children. It has lots of other code and info on child precesses and signals.

Chris
A: 

Hi Other Chris,

Unfortunately I'm working under Windows and the pcntl functions don't seem to be available (at least not in the installation package from php.net).

If I rebuild PHP with pcntl and posix under windows, will they work?

Chris

ob-so-etiquette: This is a followup to another answer, not an answer itself, so it really should be a comment on the answer it refers to.
Charles Duffy
A: 

Hi Chris, no, not without a lot of wrangling will you get pcntl and posix working; these are targeted towards Unix platforms, so you would need to recompile PHP with Cygwin. It is possible, but I personally have never got it to work.

Quite frankly (and I am a PHP devotee), I don't think you should be using PHP for command line work. There are so many problems, and you don't get to handle signals or anything like that.

Edward Z. Yang
+1  A: 

Instead of using system(), have a look at proc_open(), proc_close() and proc_terminate().

gnud