Prevent GNU screen from terminating session once executed script ends

To keep screen busy after the script completes, just keep something persistent running in a window. The simplest choice for that "something" is probably an interactive shell. Here's one way to do it (assuming bash as the choice of interactive shell):

screen -dmS session_name sh -c '/share/Sys/autorun/start_udp_listeners.sh; exec bash'
  • -dm: starts screen in detached mode
  • -S: sets session name for screen for easier retrieval later on
  • sh -c '...': instead of simply running your script, which will terminate, use sh -c to run multiple commands
  • exec bash: after the script terminates, the sh from above will switch over to an interactive shell (bash), which should never exit until something external terminates it. This will keep screen open as long as the bash instance is alive.

I had no luck with sh -c on my raspberry pi 2 running Debian 7.8. But bash -c did the job:

Command:

/usr/bin/screen -dmS test-screen bash -c "/usr/bin/top; exec bash"

Tags:

Gnu Screen