Home > other >  `read` command causes forked process to happen in foreground
`read` command causes forked process to happen in foreground

Time:12-14

Hello I am trying to write a bash script to launch QEMU in the background and wait for the user to press a key to continue with the script.

This is what I have currently:

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

However, what I get in the terminal is the QEMU console and I am unable to keep moving forward with the script. If I fork the process and don't have the read command there, it works as expected and the QEMU console does not show up and the script keeps moving forward.

Any suggestions on how I could fork the QEMU process differently or wait for user input?

CodePudding user response:

I figured it out... In bash version 4 or greater and zsh have support for this command called 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

So write the script as follows and it should launch the qemu in the background and let the script keep going forward.

#!/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

  • Related