Linux:如何检测进程是如何启动的
我的程序如何检测它是如何启动的:由某人使用命令行启动还是由另一个进程启动?
该程序有一个可选的交互部分,当它由另一个进程启动时我想禁止它 - 在后台运行;但是当它从终端启动时,我希望它执行交互操作。
[编辑] 是否可以通过 C++ 程序执行。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查您的标准输入是否
isatty
,例如Check if your stdin
isatty
, eg通常,只需提供调用者可用于在非交互模式下运行的命令行参数。你可以做更奇特的事情,但这很常见——很多时候,它 -q 表示安静。
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 有一个简单的测试,可以告诉您脚本是否是从 TTY 启动的:
Bash has a simple test that will tell you if the script was started from a TTY:
<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).