Run script in a screen

The shebang line you've seen may work on some unix variants, but not on Linux. Linux's shebang lines are limited: you can only have one option. The whole string -d -m -S screenName /bin/bash is passed as a single option to screen, instead of being passed as different words.

If you want to run a script inside screen and not mess around with multiple files or quoting, you can make the script a shell script which invokes screen if not already inside screen.

#!/bin/sh
if [ -z "$STY" ]; then exec screen -dm -S screenName /bin/bash "$0"; fi
do_stuff
more_stuff

According to the screen man pages:

  • screen -d -m Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
  • -S sessionname Set the name of the new session to sessionname.

So when I ran the command you provided: screen -dmS name ./script.sh

Screen starts a window called name and automatically runs that script.sh. To get back into there to see the status you would simply type: screen -r test

Now with Ubuntu 14.04, the commands are slightly different. Try:

screen -d -m -S test

Now for running the script, you will need to go to their config file to do so:

sudo vim /etc/screenrc

Once there, scroll down to the bottom and you will see:

# Example of automatically running some programs in windows on screen startup.
#
#   The following will open top in the first window, an ssh session to monkey
#   in the next window, and then open mutt and tail in windows 8 and 9
#   respectively.
#
# screen top
# screen -t monkey ssh monkey
# screen -t mail 8 mutt
# screen -t daemon 9 tail -f /var/log/daemon.log

This is the section where you will need to add the script name to run and that should allow you to do everything you needed from screen.


This is a bit old but one of the few threads I could find to do this. After toying arount the only way to get this running in detached mode with ubuntu 14. is

screen -d -m -t nameofwindow sh nameoflaunch.sh

The launch would be the second part above that houses the current java commands and server version. I run vanilla.