28

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.

2

2 Answers 2

34

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.

2
  • Thanks for answers, but now i faced another problem. Script stops working after i put some variables for my "program and program2"
    – TbMa
    Sep 23, 2011 at 15:30
  • This: screen -S test -d -m $HOME/folder/folder/.program almost does exactly what I'm looking for except if folder/.program is finished, the screen session is automatically closed. Is there any way to keep the screen session (shell) running even after the program is done? Perhaps instead of running the program exclusively, start a shell that starts the program and then returns to the shell prompt, or something?
    – RocketNuts
    Dec 4, 2017 at 16:28
4

Like Cry Havok mentioned, you can place the program right on the command-line.

If you really must use the -X option, then a) you need to specify the 'screen' command and b) the session needs to exist beforehand.

screen -dmS test
screen -S test -X screen $HOME/folder/folder/.program
screen -dmS test2
screen -S test2 -X screen $HOME/folder/folder/.program2
2
  • If you put the program on the command-line, screen exits when the program exits; I don't see a command-line flag to keep the screen open if you want to see the console output. If you start the screen and send the commands separately, as shown here, you may need explicit sleep commands to give screen time to start the session before sending the command.
    – Chinasaur
    Dec 15, 2014 at 18:09
  • This answer is very similar to stackoverflow.com answer which answers a similar question. I prefer this answer more than @CryHavok because this answer executes your program inside your user's default shell (@CryHavok runs your program without a parent shell so when the program dies or you ctrl-c it the screen session is terminated immediately) Apr 13, 2016 at 15:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .