You can try to create a temporary file, if it is Linux it could be something like /tmp/exe.php.lock, and delete when processing ends. But there is a great risk that the file will not be removed if PHP fails halfway.
Another output is to create a socket on a well-defined, fixed port. If bind() fails it is because another instance already created the socket on that port and has not yet left. You can refer to the example in http://php.net/manual/en/sockets.examples.php to see how to create the socket; just go to bind(), and don’t forget the socket_close() at the end.
and close the socket when the processing is over. The chance of the socket being open if PHP fails halfway is less, or perhaps non-existent.
There are even a number of different ways to do this, like the answer from fellow @epx (who already took my +1) but the impression is that you have one XY problem, that is, asking the way you think is the solution, instead of exposing the real problem.
– Bacco