Linux screen command automation Linux screen command automation shell shell

Linux screen command automation


When screen is launched without parameters, the result is that an interactive screen session is opened.

One way to achieve what you want is (assuming that the current working directory is the one containing all the VM folders):

for (( i=1; i<=33; i++ ))do   cd vm${i}   screen -dmS vm${i} qemu-system-x86_64 -kernel image -hda core-image-full-cmdline-qemux86-64.ext3 -smp 4 -m 512 -nographic --append "root=/dev/hda console=ttyS0 rw mem=512M oprofile.timer=1"   cd ..done

And here is the explanation:

For all your 33 virtual machines, enter the VM folder, and then launch a detached screen named "vmX" that keeps QEMU running.

After that, you may enter each screen by calling:

screen -r vmX

where X is the number of the virtual machine to control (e.g. kill with Ctrl + C qemu or see its stdout/stderr output).

Example:

screen -r vm1


Finally I got a solution. Thanks to user2053215 - https://unix.stackexchange.com/questions/47271/prevent-gnu-screen-from-terminating-session-once-executed-script-ends

  1. Create a shell script for executing QEMU name as "vm.sh"

    cd $1qemu-system-x86_64 -kernel image -hda core-image-full-cmdline-qemux86-64.ext3 -smp 4 -m 512 -nographic --append \ "root=/dev/hda console=ttyS0 rw mem=512M oprofile.timer=1
  2. Then we have to create another shell script, that is, the main script:

    for (( i=1; i<=32; i++ ))do cd vm$i screen -dmS vm$i sh -c "./vm.sh vm${i}; exec bash" cd ..done

  3. Now do a screen -ls. It will display all detached screens with PIDs.

  4. Do "screen -r pid "

Done :) Special thanks go to user2053215 and pietro.