Run script in a screen

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x