my aim is to invoke a shell script from a PHP program and then wait for a few seconds to send some termination key to it (I can't simply kill it because I want to test the correct execution of the termination phase).
Here is an example of what I'd like to have:
system( "RUNMYSCRIPT.sh" ); // Launch the script and return immediately.
sleep( 10 ); // Wait.
exec( "q" ); // Send a termination key to the previous script?
You need to use proc_open() to be able to communicate with your process. Your example would work like this:
You can't simply send a key event to such an external application. It is possible to write to the stdin of an external shell script by using proc_open() instead of system(), but most shell scripts listen for keystrokes directly instead of watching stdin.
What you can do instead is use signals. Virtually all shell applications respond to signals like SIGTERM and SIGHUP. It is possible to trap and handle these signals as well using shell scripts. If you use proc_open() to start your shell script then you can use proc_terminate() to send a SIGTERM signal.