Ubuntu – Run a program with GNU screen and immediately detach after

gnu-screenscripts

I am trying to figure out how to write a script which would start program(s) in GNU Screen sessions(s) at system boot.
I am trying this:

#!/bin/bash
screen -S test -d -m -X $HOME/folder/folder/.program \
screen -S test2 -d -m -X $HOME/folder/folder/.program2 \

but the command cant be executed because session is already detached?
The only thing that i need is run command in screen session and detach this session immediately.

Thanks for answers, but now i faced another problem. Script stops working after i put some variables for my "program and program2". Something like this:

#!/bin/bash
screen -S test -d -m $HOME/folder/folder/.program -f config.cfg

for some reason "-f config.cfg" got ignored. I am also tried to quote command and doesnt help too.

Best Answer

Did you really mean to put the \ at the end of the line? If not then try removing those - they escape the following character.

also, dropping the -X helps the setup work for me, for instance:

screen -S test -d -m -X touch /tmp/test

fails with No screen session found, however:

screen -S test -d -m touch /tmp/test

works fine. As such I suspect the following will work for you:

#!/bin/bash
screen -S test -d -m $HOME/folder/folder/.program
screen -S test2 -d -m $HOME/folder/folder/.program2

Remember, that if you run this at boot time, $HOME is not the same as after you log in as a specific user. If you need to run it as a certain user you'll need to use the likes of su to run it as that user, and specifying the full path will remove any ambiguity:

#!/bin/bash
screen -S test -d -m su - username /home/username/folder/folder/.program
screen -S test2 -d -m su - username /home/username/folder/folder/.program2

Or, you would call the entire script above as su - username /path/to/your/script.

Related Question