I need it to determine if hitting ctrl+d would disconnect me from server or just close current screen.
Is it somehow possible to check if I'm right now in screen session?
You can look at the $STY variable (a variable set by the screen command). If it is not "" then you are in a screen session.
I am in screen
$ echo $STY 29624.pts-1.iain-10-04 $ I am not in screen
$ echo $STY $ if test -n "$STY"; then printf "This is a screen session named '$STY'.\n"; else printf "This is NOT a screen session.\n"; fi sudo script run inside a screen You can look at the $TERM variable.
echo $TERM
If it's a screen session, the term variable should return "screen".
root@deore:/volumes# echo $TERM screen Ctrl-a -d (to exit screen)
root@deore:/volumes# echo $TERM xterm Also check: https://stackoverflow.com/questions/3472287/how-do-you-tell-if-the-current-terminal-session-is-in-gnu-screen
"$TERM" = "screen" seems to be preserved when entering sudo environment, unlike the $STY option. Unless you have changed the default key bindings, you can do Ctrl+a -> Ctrl+t, which will show the time, if you are in screen. This will work even if you have ssh:d away somewhere else, unlike the other suggestions.
^A^T is the key sequence for "nuke your homedir" in the program you're currently running? The caption command in the ~/.screenrc is a nice way to differentiate a screen session.
I'm personally using this:
$ cat ~/.screenrc caption always "%{= kc}Screen session on %H (system load: %l)%-28=%{= .m}%D %d.%m.%Y %0c" It adds a line like this one at the bottom of the screen:
Screen session on gbook (system load: 1,75 1,74 1,68) Lun 05.01.2015 13:01 With the first part (system name + load) in green and the date in pink. Useful and hard to miss!
caption always "%{= kc}Screen $STY on %H (system load: %l)%-28=%{= .m}%D %d.%m.%Y %0c" I have found another solution:
Modify your .screenrc, so my screen session looks completely different from normal terminal.
.screenrc file. Better answer (in my opinion), inspired by this, just type the following:
pstree -s $$ If you get something like this:
systemd───sshd───sshd───bash───screen───screen───bash───pstree … then you are in screen.
This is true not only for screen, but also for any kind of process (like script, nested bash or other shells) opening a nested shell, and this can even also show nested screen calls (if several not consecutive occurrences exists).
if [[ $(pstree -s $$) =~ screen ]] ; then echo 'yes' ; else echo 'no' ; fi [[ $(pstree -sA $$) =~ ---screen--- ]], this will reduce the risk of a false positive. If you are looking at a command line prompt, you can just type something, anything, and hit Ctrl+A. If your cursor jumps to the beginning of the prompt, you're not inside a screen. If you additionally have to hit A, then you are.
In bash on macOS I would use smth like this to check if I'm inside a screen session:
if [ "${TERM#screen}" != "$TERM" ]; then echo 'Inside screen' else echo 'No screen' fi Though if you want to be a tad more thorough (and esoteric I guess) you could go with a version of what @GingkoFr suggested:
if pstree -wp $$ \ | sed -E 's/^[^0-9]*([^[:blank:]]{1,}[[:blank:]]{1,}){2}//' \ | egrep -q '^screen\b' then echo 'Inside screen' else echo 'No screen' fi NB: To use latter you'll need to install pstree first. With Homebrew it's as simple as this:
brew install pstree Do a screen -ls. It's going to explicitly indicate Attached versus Detached status.
Example attached:
$ screen -ls | grep tached 3132.pts-0.esavo00 (Attached) Example detached:
$ screen -ls |grep tached 3132.pts-0.esavo00 (Detached) screen -ls to view your sessions and
screen -r sessioninfo to reconnect to a disconnected one, if detached.
screen -D -r sessioninfo to reconnect to a disconnected one.