Skip to content
Advertisement

PHP, wake up process which is sleeping

I have two PHP scripts which are running at the same time. One of them (SleepingScript.php) calls the sleep function. The other script (WakeScript.php) should be able to wake up the sleeping script if necessary.

How can I achieve this?

Here is some code that explains better the situation.

SleepingScript.php

JavaScript

WakeScript.php

JavaScript

Advertisement

Answer

This can be done using the process signal SIGCONT to inform the script it needs to wake up. Note that this requires the php extension pcntl and may be platform dependent (tested on macOS Big Sur):

SleepingScript.php

JavaScript

WakeScript.php

JavaScript

And here’s what it looks like in action. First in one terminal:

JavaScript

And going to a second terminal:

JavaScript

And back to the original terminal:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement