I have the task to start a detached screen session (e.g. when the system reboots) and then to be able to connect to it and interrupt/kill the running process without the screen to terminate. Unfortunately when I attach to the screen session and stop the running command, the entire screen session terminates with:

[screen is terminating]

I am using the following command to start the detached screen:

screen -dm -S screen_name /bin/bash -c 'cd ~/mydir; npm start'

then I attach to the screen session using:

screen -r session_name

and if I Ctrl-C the npm, the bash command will exit and the screen is terminating.

What I want is to achieve a similar behaviour when attaching to the screen as if I would have started the npm inside the screen session manually:

screen -S session_name

and then inside the screen session:

cd ~/mydir
npm start

then I can Ctrl-C the npm command without the screen session being terminated.

  • Not a good solution, but I cheat and start a second window so if I exit the first then screen doesn't terminate; eg screen -S myscreen -e ^Bb -m -d bash ; screen -S myscreen -x -X screen -t mytitle bash -c '/usr/local/bin/mycmd' – Stephen Harris Dec 22 '18 at 19:32
  • Or start the screen session differently, like bash -c 'cd ~/mydir; npm start; bash – Jeff Schaller Dec 23 '18 at 5:16
  • Potentially useful: unix.stackexchange.com/q/436959/117549 – Jeff Schaller Dec 23 '18 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.