As for me, all the magic scripts that enable and disable xDebug are quite a shamanism, but they do have a place.
If I had an urgent need to debug a production (well, suddenly) I would use a slightly different approach.
I could be wrong, but even “turned off” xDebug slows down the work. By off, I mean xdebug.remote_enable = 0. The thing is that it is loaded with a module and works accordingly.
As for the other approach. If you use php-fpm, I would create two configurations in one of which the module is not loaded, and the other module is loaded. Would launch these configurations on different ports. Further, what we call production - your web server sends to that daemon, which is without a loaded module, but for debugging - you create an additional host on the web server with a different DNS name or simply listen on another port and in this configuration send your request to the second php-fpm daemon which is just with the xdebug module loaded.
You get a very transparent thing without any megascripts.
There was a need - raised php-fpm with xDebug, need disappeared - they nailed me. And of course, with additional tools you can limit access to the debug option only on your ip, for example.
Well, of course, you don’t have any need to overload the services or the server (who is overloading the server?).