4

I'm trying to write a script to:

  1. Start a new screen session
  2. Run some program or script within that new screen session
  3. Detach from the screen session, while the program from step 2 may still be running in there. If the program from step 2 finishes, immediately or later, the screen session should remain running (detached).

I have been trying all sorts of combinations with screen -X program or screen -S somename followed by program followed by screen -D, combining with -d or -m options which I find in related questions and answers but nothing works.

The closest I could get was this:

screen -S MySessionName -d -m myprogram

This launches a new screen session in the backgroun, running myprogram. Except as soon as myprogram finishes (sometimes instantly) the screen session terminates, whereas I want to keep it running.

earthmeLon
  • 11,042
  • 1
  • 36
  • 60
RocketNuts
  • 347
  • 3
  • 4
  • 15
  • Have you checked these answers ? >> https://askubuntu.com/questions/62562/run-a-program-with-gnu-screen-and-immediately-detach-after – Rooney Dec 04 '17 at 13:09
  • @Rooney Yes, and this comes pretty close: `screen -S test -d -m myprogram` except the screen session terminates after `myprogram` is finished. I'll add this in my post for completeness. – RocketNuts Dec 04 '17 at 16:23

1 Answers1

12

Method 1

I created a demo setup you described here in my machine. I also faced the issue you reported. But adding a small line of script solved my issue.

I added the following line at the end of myprogram

exec $SHELL

After your script is finished, the Bash process will replace itself with a new invocation of itself.

Method 2

Or you can try the following method in which we start a detached screen first and send command to that screen using stuff

For this first you need to start a detached screen.

screen -dmS MySessionName

And then send the script to that screen.

screen -S MySessionName -p 0 -X stuff 'myprogram\n'

This also worked for me.

Rooney
  • 955
  • 1
  • 10
  • 25
  • 1
    Brilliant, that 2nd method is perfect! – RocketNuts Dec 04 '17 at 20:03
  • 1
    Hi, Rooney, I'm not sure but probably you could change `sleep infinity` from Method 1 to `exec bash` or `exec $SHELL` - analogically to the solutions provided here: [With a launcher for a terminal application, how can I keep the terminal open after the program is complete?](https://askubuntu.com/q/3359/566421) – pa4080 Dec 05 '17 at 09:19
  • 1
    Hi, @pa4080 You are right. thanks for the reply. Now the screen stays detached and i can continue to the screen, while using `sleep infinity` the only available option is terminate the screen manually. – Rooney Dec 05 '17 at 09:43
  • The second method is perfect for when my script crashes and doesn't get around to invokating itself. Many thanks! – Scott Hather May 16 '20 at 13:53
  • When I use method 2 the screen hasn't actually executed the `myprogram` -- it has just typed this into the screens terminal. Is there a way to also execute it? – David Jan 09 '23 at 18:56