0

I'm have some php code:

<?
$cmd="mkfifo /tmp/myfifo;";
system($cmd);
$cmd="echo 1 > /tmp/myfifo 2>&1 &";
system($cmd);
?>

on an apache server. I want to have the second command not block. According to the info page of system:

If a program is started with this function, in order for it to continue running 
in the background, the output of the program must be redirected to a file or 
another output stream. Failing to do so will cause PHP to hang until the execution 
of the program ends.

But I don't see how to apply that to this situation. I tried

$cmd="echo 1 > /tmp/myfifo > /dev/null 2>&1 &";

But to be honest that seems nonsensical.

EDIT:

My ultimate goal is to write to a fifo that may never be read from, and time out the write after 5 seconds. So, if I can manage to get this command to not block the php executition, I can sleep 5 seconds and then cat /tmp/myfifo > /dev/null 2>&1 to unblock the original write.

Can anyone think of a better way to have my write not hang indefinitely (in neither the background nor the foreground)?

4
  • What is your actual goal? This sounds like a bad approach. Commented Feb 7, 2012 at 20:06
  • My actual goal is to write to a fifo that is read by a server process. If the server process is not running the fifo will not be read, but I don't want that to force php to hang, or leave a bunch of hanging background processes. Commented Feb 7, 2012 at 20:09
  • Can you not just use PHP's streams and file functions to write or append to that file? Any reason you are using system() calls? Commented Feb 7, 2012 at 21:05
  • Have you ever found a solution to this problem. I can see that the accepted answer below does not really solve the problem. Commented Oct 6, 2018 at 16:07

3 Answers 3

2

If the environment created by system() is bash 4.x or later I believe you could use "coproc" to prevent it from blocking? This works for me in shell scripts. (Or, if it's not executing in bash directly, you could have it call "bash -c ..." )

Sign up to request clarification or add additional context in comments.

Comments

1

If you need to write to a file (and prevent other processes from writing until you are done), then just use file_put_contents.

<?php
// The new person to add to the file
$person = "John Smith\n";

// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents('/tmp/myfifo', $person, FILE_APPEND | LOCK_EX);

5 Comments

This still blocks until the fifo pipe is read. Is there a way to have a timeout on the operation or anything?
What do you mean it "blocks until the fifo pipe is read"? That code should open (or create if missing) that file, write the added value down, then close it. It should only take a couple hundred ms at most. What else are you doing? Are other processes writing lots of data to this file? You can remove the | LOCK_EX if you don't need it.
I did remove it, though this is the only process writing to the file anyway. The entirety of my php file is file_put_contents('/tmp/myfifo', "1", FILE_APPEND) in between the open and close tags. When I try to open the page in a browser, the browser sits there loading, and will never stop unless in a terminal I execute cat /tmp/myfifo.
I think the key point here is that I want to write to a fifo pipe, not a regular file.
@Snitse, I don't have experience writing to fifo's and it sounds like that resource is "blocking". However, you can use the stream wrappers to write to a resource (like a fifo) in a non-blocking mode. See the manual for more information.
0

You could use nohup to launch a process in the background:

$ nohup binary arguments
$ man nohup

1 Comment

I tried $cmd="nohup echo '1' > /tmp/myfifo 2>&1 &"; system($cmd); But it still blocked php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.