Sunday, April 28, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 895  / 3 Years ago, sun, june 27, 2021, 12:48:15

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.


More From » command-line

 Answers
1

Running screen -xR will do basically the same as running screen -xr || screen.



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 .screenrc.



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 chsh, then entering your password, then typing in /bin/bash and pressing enter.



Next edit the hidden .bashrc file in your home folder, adding the following to the end:



if [ ${SHLVL} -eq 1 ]; then
((SHLVL+=1)); export SHLVL
exec screen -xR
fi


Now, edit (create if it doesn't exit) the hidden file .screenrc in the home folder. Add or edit the following settings:



shell /bin/zsh
startup_message off


(Of course, if you want some other shell than Zsh, go ahead and put that in instead. It should even work with bash.)


[#43949] Monday, June 28, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
oatglori

Total Points: 313
Total Questions: 102
Total Answers: 111

Location: Guam
Member since Thu, May 18, 2023
1 Year ago
oatglori questions
;