How to start several jobs in different screen sessions in a Bash-script?

6,408

For a single session, I use something like that:

cat > screenrc-test <<EOF
screen -t test1 sh -c "./myprogram < input_part1.txt > output_part1.txt"
screen -t test2 sh -c "./myprogram < input_part2.txt > output_part2.txt"
screen -t test3 sh -c "./myprogram < input_part3.txt > output_part3.txt"
screen -t test4 sh -c "./myprogram < input_part4.txt > output_part4.txt"
EOF

screen -S test-all -c screenrc-test

I don't know why you want several sessions, but the syntax is, in a similar way:

screen -mdS test1 sh -c "./myprogram < input_part1.txt > output_part1.txt"

for each session. Using a shell is necessary for the redirections, otherwise the redirections would be applied to the screen command instead of myprogram.

An example:

#!/bin/sh
screen -mdS test1 zsh -c "repeat 4 { date; sleep 1; } > out1"
screen -mdS test2 zsh -c "repeat 4 { date; sleep 1; } > out2"
screen -mdS test3 zsh -c "repeat 4 { date; sleep 1; } > out3"

After running it (and waiting for 4 seconds), I get:

==> out1 <==
Tue Sep  2 09:23:07 CEST 2014
Tue Sep  2 09:23:08 CEST 2014
Tue Sep  2 09:23:09 CEST 2014
Tue Sep  2 09:23:10 CEST 2014

==> out2 <==
Tue Sep  2 09:23:07 CEST 2014
Tue Sep  2 09:23:08 CEST 2014
Tue Sep  2 09:23:09 CEST 2014
Tue Sep  2 09:23:10 CEST 2014

==> out3 <==
Tue Sep  2 09:23:07 CEST 2014
Tue Sep  2 09:23:08 CEST 2014
Tue Sep  2 09:23:09 CEST 2014
Tue Sep  2 09:23:10 CEST 2014

showing that the commands are run in parallel.

Share:
6,408
Chrispie
Author by

Chrispie

Updated on September 18, 2022

Comments

  • Chrispie
    Chrispie over 1 year

    The program I want use can be started via

    ./myprogram < input_part1.txt > output_part1.txt
    

    How can I start four jobs of myprogram in a BASH-script where each job runs in a separate SCREEN-session? The sessions do not exist before I start the script.

    I tried:

    #!/bin/bash
    screen -mdS test1 ./myprogram < input_part1.txt > output_part1.txt
    screen -mdS test2 ./myprogram < input_part2.txt > output_part2.txt
    screen -mdS test3 ./myprogram < input_part3.txt > output_part3.txt
    screen -mdS test4 ./myprogram < input_part4.txt > output_part4.txt
    

    but this does not work. Why not?

    • tijagi
      tijagi over 9 years
      > 2014 > still using screen
    • Chrispie
      Chrispie over 9 years
      @tijage: solutions with an alternative are also welcome. But I can not guarantee, that this solutions run on the machines I want to use.
  • Chrispie
    Chrispie over 9 years
    I think in that case all jobs will be started successively, right? So, after ./myprogram < input_part1.txt > output_part1.txt is finished ./myprogram < input_part2.txt > output_part2.txt will be started?
  • vinc17
    vinc17 over 9 years
    @Chrispie No, the commands are run in parallel. I've just added an example. The reason is that in both cases, the screen lines (the ones in the screenrc or the ones with the -md option) return immediately.
  • Chrispie
    Chrispie over 9 years
    Ok, thanks. This is exactly what I was searching for.