将键盘输入发送到正在运行的 Linux 进程
我正在为 mp3 播放器(mpg123 linux)开发网络界面。 mpg123 是一个命令行 mp3 播放器,可以使用键盘输入进行控制。例如:
$ mpg123 -C filename.mp3
它将开始播放歌曲并监视键盘输入以进行控制。按 's' 将暂停歌曲 'q' 以退出等。
我正在使用 Perl 脚本生成 mpg123 进程。我想从该脚本向该流程发送输入。我有进程的 pid,我只需要将击键发送到该进程以进行控制。
I am developing web interface for an mp3 player (mpg123 linux). The mpg123 is a command-line mp3 player and can be controlled using keyboard inputs. For example:
$ mpg123 -C filename.mp3
it will start playing the song and monitor keyboard inputs for control. Pressing 's' will pause the song 'q' for quit etc.
I am spawning an mpg123 process using a Perl script. From that script I want to send inputs to this process. I have the pid of the process, I just need to send keystrokes to this process for control purpose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需从 Perl 中将 mp3 播放器作为管道生成即可。像这样:
第二次尝试多个脚本调用:在交互式 shell 中输入
tty
。这将为您提供一个伪终端名称。现在在这个 shell 中启动你的播放器。在另一个 shell 中,写入该伪终端。例如echo "s" > /dev/pts/11
。玩家将收到此信息作为输入。如果这有效,请使用 Perl 脚本而不是 echo 写入伪终端。You just have to spawn your mp3-player as a pipe from perl. Like so:
Second try for multiple script invocations: In an interactive shell enter
tty
. That will give you a pseudo-terminal name. Now start your player in this shell. In another shell, write to that pseudo-terminal. E.g.echo "s" > /dev/pts/11
. The player will receive this as input. If this works, use your perl script instead of echo to write to the pseudo-terminal.