Confusion_buddy
Confusion_buddy

Reputation: 348

Linux screen command automation

I am using the Linux screen command to run different directory files one by one. I have 33 VM folders, and each folder contains images to execute.

Root directory      = /home/root/
VM folder available = /home/root/vm1,vm2,vm3...vm32

I have to run all VMs at the same time. For that reason I am using the screen command. Each screen command will execute on VM. It should traverse all 33 VM folders and execute all 33 VM images at the same time.

Ctrl + A, C = new screen

Following is my code:

     for (( i=0; i<=33; i++))
        do
        screen
        ls
        vm1 vm2 vm3 vm4 ....vm33
        cd vm1
        ls
        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 ..
        screen
        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

QEMU exits as soon as it is launched, and screen with it. How can I fix this issue?

Upvotes: 5

Views: 3358

Answers (2)

Confusion_buddy
Confusion_buddy

Reputation: 348

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 $1
    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
    
  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.

Upvotes: 0

Pietro Richieri
Pietro Richieri

Reputation: 51

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

Upvotes: 5

Related Questions