如何在UNIX中检测到前台进程正在等待输入? [英] How to detect that foreground process is waiting for input in UNIX?

查看:56
本文介绍了如何在UNIX中检测到前台进程正在等待输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个脚本(ksh或perl)来启动一定数量的并行作业(另一个脚本),每个并行作业在单独的会话中作为前台进程运行.另外,我开始监视作业,该作业必须确定这些脚本中的任何脚本是否期望操作员输入,并在必要时切换到相应的会话. 我的问题是,我还没有找到确定该过程是否需要输入的好方法.对于后台进程,这非常简单:进程状态为已停止",可以使用"ps"命令轻松进行检查.在前台处理的情况下,此操作不起作用. 到目前为止,我尝试使用dbx或truss附加到进程,以查看它是否挂在读取"上,但是这种方法似乎太繁重了. 您能提出一些更好的解决方案吗? Perl,shell,C,Java等...只要是标准的,就可以了,不需要额外的第三方或特定于操作系统的东西即可安装.

I have to create a script (ksh or perl) that starts certain number of parallel jobs (another scripts), each of them runs as a foreground process in a separate session. Plus I start monitoring job that has to determine if any of those scripts is expecting input from operator, and switch to the corresponding session if necessary. My problem is that I have not found a good way to determine that process is expecting input. For the background process it's pretty easy: process state is "stopped" and this can be easily checked with 'ps' command. In case of foreground process this does not work. So far I tried to attach to the process with dbx or truss to see if it's hanging on 'read', but this approach seems too heavyweight. Could you suggest some better solution? Perl, shell, C, Java, etc. … is ok as long as it’s standard and does not require extra 3rd party or OS-specific stuff to install.

谢谢.

推荐答案

您要问的事情是不可能的,至少是不可靠的.该过程可能使用 select 或其他轮询方法,而不是阻止read呼叫.您不知道它是在等待操作员输入还是在忙着做其他事情,并且一般来说,两者都可能是(在响应操作员输入的同时在后台做一些事情).

What you're asking isn't possible, at least not reliably. The process may be using select or other polling method rather than blocking on a read call. You can't know whether it's waiting for operator input or busy doing other stuff, and in general it could be both (doing stuff in the background while being responsive to operator input).

程序发出等待操作员输入信号的正常方式是打印提示.因此,如果自从上次喂入会话输入以来,它一直显示提示,则应认为该会话处于活动状态.

The normal way for a program to signal that it's waiting for operator input is to print a prompt. Thus you should consider a session to be active if it's displayed a prompt since the last time you fed it input.

如果您的程序不以这种方式运行,则需要找到其他特定于程序的方式,以了解这些进程正在等待输入.

If your programs don't behave this way, you'll need to find some other program-specific way to know that these processes are waiting for input.

这篇关于如何在UNIX中检测到前台进程正在等待输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆