Reputation: 7155
How can my program detect how it was started: by someone using the command-line or by another process?
The program has an optional interactive part that I want to suppress when it was started by another process - running in the background; but when it was started from a terminal I want it to do the interactive bit.
[edit] If it is possible to do from a C++ program.
Upvotes: 1
Views: 471
Reputation: 14326
<unistd.h> defines the isatty function that you could use to check if the input (0) or output (1) file descriptors are connected to a terminal (which means it is an interactive session).
Upvotes: 1
Reputation: 36402
Check if your stdin isatty
, eg
if (isatty(0))
{
/* interactive! */
}
Upvotes: 3
Reputation: 496
Bash has a simple test that will tell you if the script was started from a TTY:
if [ -t 0 ]; then
echo "Interactive code goes here"
fi
Upvotes: 1
Reputation: 89152
Usually, just provide command-line arguments that the caller can use to run in non-interactive mode. You can do fancier things, but that's pretty common -- a lot of times, it -q for quiet.
Upvotes: 1