Reputation: 187
I have some PHP code that I need to start a ruby script in the background on the server. But the script doesn't want to work. All the script does for now is get input and echo it back.
I invoke it like so:
shell_exec("./fifo_test.r < input.fifo > buffer &");
This causes the PHP to hang indefinitely. But it works fine when I run it directly.
root@ip-xxxxx:/var/www/test# su www-data
$ sh -c ./fifo_test.r < input.fifo > buffer &
$ echo test > input.fifo
$ cat buffer
Got: test
Here is the process info:
ubuntu@ip-xxxxx:~$ ps -ef | grep fifo
www-data 1076 1 0 00:39 ? 00:00:00 sh -c ./fifo_test.r < input.fifo > buffer &
Any thoughts? Thanks!
EDIT: I can unfreeze the PHP by manually adding to input.fifo. Then it works fine afterward. But it waits for that first input forever when PHP starts it. I am not sure how to avoid this!
Upvotes: 0
Views: 390
Reputation: 831
You may try using the nohup
command in your shell call. This should cause the program to return immediately so PHP isn't waiting. The code in your case would look like so:
shell_exec("nohup ./fifo_test.r < input.fifo > buffer &");
Hope that helps!
Upvotes: 1
Reputation: 32112
If the PHP script is generating the input, use popen(). That way, you can avoid using a named pipe.
If the input is coming from some other source, it is best not to have your PHP script (or the Ruby script it executes) read directly from a FIFO. I know this from trying to write a Web interface for displaying captured network packets in real-time years ago and encountering a similar problem.
I would instead write a simple server program that accepts Unix socket connections. This server can accept the input either from a FIFO or another socket connection and then send it to all running instances of the PHP or Ruby script. You can write such a server in the CLI version of PHP using only core features, although Node.js (for JavaScript), Twisted (for Python), and EventMachine (for Ruby) are designed for this sort of thing.
Upvotes: 1