I'm writing a Wordpress plugin that runs a PHP script on initiation and listens for events continuously using WebSockets and logs connections.
e.g.
shell_exec('php restserver.php >restserver.log 2>&1 &');
The script mentioned is outside of the Wordpress API. Im aware using exec
or shell_exec
is an extreme method (often the answer to a worng question).
I have managed to record the restserver.php process ID to be able to kill it if the plugin is disabled and to not re-launch the script if the plugin is reinitiated.
So in short...
- Is there a better way to run a background php process that is independant of wordpress?
- Is there a "best practice" way to detect the plugin disable event and launch the kill process command before this?
EDIT: The secret to question number 2, apparently lies in using register_deactivation_hook()
after register_activation_hook()
, to register your class or functions that make the magic.
It worked for me anyway. Any further information about using exec()
the right way is always welcome.