0

I would like to run a bash scrpit while using a screen session, here is my script :

#!/bin/bash
for i in 1 5 18 20
do
screen -S output_${i}
./run_my_program
screen -d

The problem is that the screen session does not detach using screen -d (but detach with the keyboard shortcut ctrl-a d), any suggestion ? Thanks.

  • 1
    That also probably doesn't start the ./run_my_program within the screen, but waits for the first screen to return? Do you mean you want to start a detached screen session with some command? – ilkkachu Jul 27 '17 at 11:59
  • @ilkkachu actually it start the program but don't close the screen session. – liquid-snake Jul 27 '17 at 12:03
3

You don't have to "enter" the screen session to get it to run, just use -dm and it will start the session in detached mode:

for i in i 5 18 20; do
    screen -dm -S "output_$i" ./run_my_program
done
  • Thanks for your help, "output_${i}" is the name of my screen session write. – liquid-snake Jul 27 '17 at 12:03
  • @liquid-snake right, the -S argument names the session – Eric Renouf Jul 27 '17 at 12:04
  • What to do if I need to add an extra command before running my program, like export OMP_NUM_THREADS=1 && ./run_my_program ? – liquid-snake Jul 27 '17 at 12:05
  • @liquid-snake screen will inherit values from the parent, so you could always set/update the OMP_NUM_THREADS variable prior to launching, or if the script could take that as an argument, you could pass it that way, or you could run a shell to set the variable for you: screen ... sh -c "OMP_NUM_THREADS=1 ./run_my_program" – Eric Renouf Jul 27 '17 at 12:15
  • Thanks, it's working fine now, as you suggest I just run OMP_NUM_THREADS=1 befor the screen -dm -S ...... Thanks again Eric. – liquid-snake Jul 27 '17 at 13:00

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.