【问题标题】:`read` command causes forked process to happen in foreground`read` 命令导致分叉进程在前台发生
【发布时间】:2022-01-15 12:16:59
【问题描述】:

您好,我正在尝试编写一个 bash 脚本以在后台启动 QEMU 并等待用户按键以继续执行该脚本。

这是我目前拥有的:

setup_for_greengrass # these are functions
run_qemu & # fork function and try to run in the background
echo "Press anything to continue once VM is finished booting...\n"
read fullname # wait for user to press a key
install_greengrass

但是,我在终端中看到的是 QEMU 控制台,我无法继续使用该脚本。如果我 fork 进程并且那里没有 read 命令,它会按预期工作并且 QEMU 控制台不会显示并且脚本会继续前进。

关于如何以不同方式分叉 QEMU 进程或等待用户输入的任何建议?

【问题讨论】:

  • 那么在没有控制台的情况下运行 qemu?
  • @KamilCuk 我想在后台运行 qemu,这样它就不会阻塞我的脚本的其余部分

标签: bash shell terminal qemu


【解决方案1】:

我想通了...在 bash 版本 4 或更高版本中,zsh 支持这个名为 coproc 的命令。

https://www.geeksforgeeks.org/coproc-command-in-linux-with-examples/

https://stackoverflow.com/a/68407430/4397021

https://www.zsh.org/mla/users/2011/msg00095.html

所以编写脚本如下,它应该在后台启动 qemu 并让脚本继续前进。

#!/bin/zsh # Make sure to use zsh or upgrade your version of bash

setup_for_greengrass 
coproc run_qemu # launch qemu in the background 
echo "Press anything to continue once VM is finished booting...\n"
read fullname # wait for user to press a key
install_greengrass

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多