gpt4 book ai didi

linux - 是否可以将 "push"消息发送到事件的 bash 终端?

转载 作者:行者123 更新时间:2023-12-04 18:37:28 25 4
gpt4 key购买 nike

例如通知用户某些任务的状态或报告事件。这不仅应该在登录时发生,而且应该在 session 的整个生命周期中发生。
如果消息显示被推迟到用户退出应用程序并返回到 shell 时,这是可以的。

通用的 Linux 解决方案是首选,但仅适用于 Ubuntu 的解决方案也可以。

最佳答案

首先,一些背景。
每个终端都有一个相关的终端设备文件,您可以使用 tty命令

 % tty
/dev/pts/2

如果程序写入此文件,它将出现在终端上。这是一个例子
 % echo "Hi there" > /dev/pts/2
Hi there

现在,您是否可以写入此文件取决于文件的权限
 % ls -l /dev/pts/2
crw------- 1 noufal tty 136, 2 Mar 4 10:32 /dev/pts/2

这意味着我可以写入但其他人无法写入(这是合理的,因为我不希望其他人在我的终端上写入)。像 wall 这样的命令Prashanth 提到,无论权限如何,都写入所有用户的终端文件,因为它以 tty 用户的组权限运行,并且所有终端设备文件也具有相同的组。注意 s在下面组的执行权限中。
% ls -l /usr/bin/wall
-rwxr-sr-x 1 root tty 27K Mar 7 2018 /usr/bin/wall

单独地,您可以使用 ` 打开或关闭到终端的广播
% mesg
is n
% ls -l /dev/pts/2
crw------- 1 noufal tty 136, 2 Mar 4 10:35 /dev/pts/2
% mesg y
% ls -l /dev/pts/2
crw--w---- 1 noufal tty 136, 2 Mar 4 10:35 /dev/pts/2

现在,回答你的问题,如果我写信给 tty给定 shell 的文件,无论那里运行什么,我都可以在该终端中显示内容。需要注意的是,您需要与运行 shell 的用户是同一用户,或者您的程序必须以 sgid tty 身份运行。

这是一个简单的带注释的 shell 函数,它将在给定的终端上显示正在运行的时钟。
function dclock () {
# Both the tput and date redirect their output to the given tty
# file so that the display occurs over there.
while true
do
tput cup 0 50 > $1 # Position the cursor at row 0 column 50
date > $1 # Print the date
sleep 1
done
}

您可以打开不同的终端 (T),使用 tty 查找 tty 文件命令然后运行 ​​ dclock /dev/pts/whatever在您当前的终端中查看时钟出现在 T 中。

关于linux - 是否可以将 "push"消息发送到事件的 bash 终端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60519149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com