Search code examples
bashdockershellinitialization

How to start a screen session from entrypoint shell script, and make it persist?


I am starting a screen session in my entrypoint shell script in docker, then log the output of screen -list to a file.

#!/bin/bash

screen -S my_screen -dm bash -c 'cd project && npm run start'

screen -list > scr_list.log

Then I connect to the docker container and see the outputted log.

here is a screen on: 
        14.my_screen      (12/18/24 17:55:29)     (Detached) 
1 Socket in /run/screen/S-root.

But when I manually run screen -list after connecting, it outputs:

No Sockets found in /run/screen/S-root.

Using the same user (whoami): root

I currently have multiple screen services running inside the container and I manually restart individual ones from time to time whenever I make some changes in the container

Any ideas, what could be the issue please?

Update

screen seems to persist, but once I add npm run dev, the screen does not persist. When I run the same script from inside the container, it works.


Solution

  • I figured out the issue: npm was not found.

    As suggested in the comments, I used read -p to pause the script and found out eventually that screen was persisting, but got terminated.

    Update

    To add node and npm to your $PATH variable, without needing to know the version number, use:

    source /root/.nvm/nvm.sh
    

    Original solution

    By running which npm I found the path to my npm installation (version), then added it to the $PATH in my script.

    export PATH=$PATH:/path/to/npm
    

    (folder in which npm executable is located)