2

I'm running a continuous PHP loop that executes another PHP file by using exec("php ...");. The plan is for the executed script to run, then sleep for 2 seconds, then start again. However, it seems like my loop is starting a new instance every 2 seconds instead. So long question short, how do I get my first php script to wait until the execution of script nr 2 is complete?

All this is run using the command line. I would also like the echo functions in script nr 2 to show up on the command line.

Any thoughts would help.

Thanks

2 Answers 2

2

Exec does not maintain any state information between instances. You could:
Loop in your subscript
OR
You could set some sort of environment variables or that are read at the beginning of the subscript and written at the end.
OR
You could have the subscript read/write to a file in a similar fashion
OR
You could pass in parameters to the subscript who's output is captured

For outputting to the screen, you might play around with the other exec/system calls:
exec
shell_exec
passthru
system

I believe passthru() will work. Another possibility if it doesn't is to call exec(), using the output parameters to capture the output strings from the subscript. Then just echoing that output on return of the subscript.

I also believe that using the output parameters (or capturing the result of the function in a variable) will cause the exec to wait until the command is complete before continuing on.

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

1 Comment

That's a very interesting and likely theory. I'll test it out later
0

The problem is, once you excute the script, it will run. Another exec will start another instance like you found out.

What you can do is Put the sleep inside the executed script. Once it starts running, it will do its own sleep. You can look at setting an execution time limit and maybe ignoring user abort. You can create a function and let your script call that function. It will then sleep after execution and call the function again.

 
// maybe set time limit here
Function loop ()
{

Sleep(120); 
//you can make a check whether to loop or not.
Loop(); 
}

Loop(); 

Comments

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.