Creating a shell script for use with 'screen'?

15,168

As stated in SYNOPSIS of man screen if you want to run a command in screen it needs to be appended to the command issuing screen. So in your case it would look like

 #!/bin/sh
screen -S tf2 ./startserver.sh

Yet as I do not know if your shell-script is in the working directory of screen, It is more clever to do this

 #!/bin/sh
screen -S tf2 /full/path/to/startserver.sh
Share:
15,168

Related videos on Youtube

S3ntri
Author by

S3ntri

Updated on September 18, 2022

Comments

  • S3ntri
    S3ntri about 1 year

    I have been working on creating a shell script for a while, browsing various forums including this one, and using all sorts of search terms with no luck. I hope I am not creating a duplicate question.

    We have a game server and we have to initialize screen manually after a reboot or a crash, but would like to pack our few commands into one script to lower the downtime. What I would like to do is to make a script that creates a screen session, switches to it, and executes the shell script within the new session.

    I have tried the code below in the shell script.

    #!/bin/sh
    screen -S tf2
    ./startserver.sh
    

    When I execute the script, it creates the screen 'tf2' and enters the session as I wanted. The server does not start where I wanted it to however, it boots up outside of the screen session where I executed it. We need it to be inside the screen session so that our game server can be online without anyone attending the terminal.

    I am not the best with shell scripts yet but I am willing to learn, am I doing something wrong? Is there another way I should be going about this?

  • S3ntri
    S3ntri almost 7 years
    This helped a lot, it worked like a charm. I was running this in the working directory of the script I was attempting to execute. Thank you!