How can I set screen so it runs automatically any time a user logs in using a CLI interface? If there is already a screen session running for that user, I would like any new logins to attach to that screen session. It would be really nice if on attaching to the screen session a new window is created for each new login.
First, for the users you are going to do this for, make sure their
(or whatever other shell your user currently uses). If you do not have this setting set and you change their shell like we will, the user will not be able to login. You might also consider making the screen startup message go away with this line
Ok, now lets make a runnable script that attaches to the first already running screen session if it exits, and it none is already running it starts one. Run in a terminal
Put in the two following lines:
Press Ctrl x and then y to save the file. Run
The problem is that as far as I know, you can't pass flags to a login shell. Also, having something non-shell like screen as your login shell would probably break things like SFTP.
So, what we'll do is set bash as our login shell. Then we will add to the end of .bashrc (a file bash runs when you start it) code that runs screen. Screen will then run whatever shell is specified in
We will make it so that this command will run only under a special condition: Only if the current running copy bash has not been started (possibly indirectly) by another copy of bash. This will make it so that if you user wants bash as their shell, and screen starts bash inside of it, the user won't get a Russian dolls explosion of bash inside of screen inside of bash inside of screen inside of bash until the computer runs out of memory.
First, change the shell to /bin/bash, by running
Next edit the hidden
Now, edit (create if it doesn't exit) the hidden file
(Of course, if you want some other shell than Zsh, go ahead and put that in instead. It should even work with bash.)