2

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?

  • 1
    > 2014 > still using screen – tijagi Sep 1 '14 at 11:58
  • @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 Sep 1 '14 at 12:06
3

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.

  • 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? – Chrispie Sep 2 '14 at 7:19
  • @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. – vinc17 Sep 2 '14 at 7:32
  • Ok, thanks. This is exactly what I was searching for. – Chrispie Sep 2 '14 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.