I'm trying to determine whether a command I'm running is within an SSH session. Usually this works fine by checking for $SSH_CONNECTION
or walking the process tree and looking for sshd
.
However, if I start a screen
session locally and then re-attach it through SSH, neither of those works.
Is there some way from within the reattached screen session to determine which shell the session is currently attached to?
The process tree just looks like shell(X) --> screen(Y) --> systemd(1)
, which makes sense, since the screen session probably gets reparented when I exit the local terminal.
screen -ls
does not say anything more than (Attached)
, with only the PID Y
, no helpful PID of where it is currently attached.
The process tree of shell(A)
where it is attached includes a single child screen(B)
, but I cannot find a way to link the PIDs Y
and B
. I even tried to find the other end of the unix socket being used by screen but it comes up empty. (even checked as root
).
Is this just something that isn't possible?
ps waux | grep screen
might be useful. You could grep against user. Depending on your version/OS,who
might be helpful.tty
, which will show you the terminal shell running in thescreen
, orps T
which will show you the currently running processes only within the current shell, something that you are looking for?dev/pts/3
from inside screen, but there's no way to link that to the screen process that initiated the re-attach. ps did list the process with the re-attach command, but that would require argument parsing in a manner that won't really scale.ps -q $(ps h -q $(($$-1)) -o ppid) -o tty,cmd,pid
which will give you thetty
andpid
of the original shell that invokedscreen