Let me first say that I do not think this is the right way to approach your problem. At all. There must be a better solution for a particular application, and you really have to find it.
The main problem is that, not knowing which programs you want to "force close", you will not know how to make them "exit the system." And if you know the software, the CodeGnome solution seems great.
Also note that not all programs read input directly from STDIN. For example, the Python getpass module opens an additional file descriptor directly to /dev/tty , so closing or redirecting STDIN does not matter. It also means that sudo accepts passwords, and I believe that ncurses works that way. Reading in this way allows you to receive passwords without displaying them on the screen.
For hints that are not directly readable from /dev/tty (e.g. bash read ), just redirecting STDIN from /dev/null (e.g. ./prompt.sh </dev/null ) can take you somewhere. In addition, you can do what you do and close STDIN, but how the program works when it detects a closed STDIN or a redirected one consisting only of EOF,. I hope the program will be mistaken as you wish, but perhaps it will continue the cycle, waiting for the actual input. Who knows?
Moreover, neither closure nor redirection worldwide will work if that happens. Some programs may fail because you are hoping for closed STDINs; others may need EOF from /dev/null
Thus, the way to do this is not a general, after all, type of solution, but one intended for the programs that you placed in this place. Even better, do not use try (to run the program), then catch (the case when the program asks for input), just call the programs in ways that you know they will not ask for input.
In any case, all that is said, one of the possibilities that can work is to close STDIN and execute the background process. For instance:
#!/bin/bash exec 0<&- ./prompt.sh &
or, conversely, redirect STDIN to / dev / null and execute the background process. For instance:
Then you will need to check the return code of the programs (using $? ) To make sure they exit correctly. (We hope that the unknown list of programs that you use matches the standard return value scheme)
Closing / redirecting STDIN will handle the case when STDIN is used to receive input and background processing the process will handle the case where /dev/tty (since the background process does not have tty).
You yourself, if the program offers you to use some other method (opens FD directly to your pseudo-terminal, a window with graphic input pops up, presents you with a sound prompt, etc.).