我编写了一个shell脚本,它使用trap特性在接收到SIGUSR1信号时输出其进度,但我不知道如何让脚本能够将进度输出到发送信号的进程的STDOUT。因此,如果脚本在终端模拟器/dev/pts/10中运行,而我从/dev/pts/11上的终端仿真器向进程发送信号,则输出将转到/dev/pts/10,但我希望发送到/dev/pts/11。我不想求助于通过wall发送进度消息或硬编码的shell重定向到特定的STDOUT。
发布于 2020-06-24 18:41:42
你想要的是不可能的。进程无法确定哪个进程向其发送信号:如果多个进程在足够短的时间内发送相同的信号,则接收方只会看到单个信号。
即使您知道是哪个进程发送了信号,您也可能没有写入发送方标准输出的权限。
一种合理的方法是让进程侦听Unix套接字。当此套接字上有连接时,请在该套接字上写入进度信息。让另一端负责打印它想要的地方。
要查询状态,可以使用在某些发行版中找到的socket实用程序,或者更强大但更复杂的socat。
socket -r ./my-program-status # If the socket is a filesystem domain socket in the current directory
socat UNIX-CLIENT:my-program-status - # If the socket is a filesystem domain socket in the current directory
socat ABSTRACT-CLIENT:my-program-status - # If the socket is an abstract domain socket in the current directory服务器端概念证明:
while date | socket -q -s ./status; do echo .; done另一种方法是让您的程序定期将其状态写入文件。
https://unix.stackexchange.com/questions/594867
复制相似问题