开发者

Linux: how to detect how a process was started

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 possi开发者_运维技巧ble to do from a C++ program.


Check if your stdin isatty, eg

if (isatty(0))
{
    /* interactive! */
}


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.


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


<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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜