23

I want to run a bash script in a detached screen. The script calls a program a few times, each of which takes too long to wait. My first thought was to simply open a screen and then call the script, but it appears that I can't detach (by ctrl-a d) while the script is running. So I did some research and found this instruction to replace the shebang with following:

#!/usr/bin/screen -d -m -S screenName /bin/bash

But that doesn't work, either (the options are not recognized). Any suggestions?

PS It occurs to me just now that screen -dmS name ./script.sh would probably work for my purposes, but I'm still curious about how to incorporate this into the script. Thank you.

25

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
  • It works well, what is the "$0" for here ? – Fabich Dec 19 '16 at 17:37
  • 4
    @Lordofdark That's the script name. The script invokes screen which invokes /bin/bash which invokes the script again. – Gilles Dec 19 '16 at 18:33
16

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.

  • Somehow that command (screen -dmS ...) doesn't work for me, even though it really should. I run it and then screen -ls and no sockets are found. Thoughts? – bongbang Oct 14 '14 at 22:45
  • What distro are you using? – ryekayo Oct 14 '14 at 22:46
  • NAME="Ubuntu" VERSION="14.04.1 LTS, Trusty Tahr" I think. It's a virtual machine. – bongbang Oct 14 '14 at 22:49
  • Gimme a bit because im just leaving work and ill try to use screen on my machine which is also ubuntu 14.04 – ryekayo Oct 14 '14 at 22:50
  • @bongbang - what version of screen do you have? – slm Oct 14 '14 at 23:45
3

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.

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.