Typically, $STY
will be set.
So:
if [ -n "$STY" ]; then
echo "I'm most likely running under screen"
fi
$STY
is typically what you need to talk to your screen
. That is used to construct the path of the Unix domain socket used to control screen
(something like /var/run/screen/S-$USER/$STY
.
Now, that won't work for instance if from that screen, you ssh
to another machine. The shell started there won't have $STY
in its environment, and that wouldn't be of any use to it anyway, since it wouldn't be able to access the Unix domain sockets on your machine.
However, it is possible to query the terminal with an escape sequence to determine its type:
if [ -t 1 ] && [ -t 0 ]; then
s=$(stty -g)
stty -icanon -echo min 0 time 3
printf '\033[>c'
type=$(dd count=1 2> /dev/null)
stty "$s"
case $type in
(*'>83;'*) echo "this is screen"
esac
fi
Another approach, as suggested by @val0x00ff is to check the value of the $TERM
environment variable. That value is meant to tell applications what type of terminal they're talking to. It is set by screen
to something that starts with screen
(as screen
can implement different variants of terminals). $TERM
is passed accross rlogin
, rsh
, telnet
, ssh
. It's not as guaranteed to work as the above but is simpler and less intrusive.
case $TERM in
(screen*) echo "I'm more than likely running in screen"
esac
Like for the previous solution, you may be running in screen
, but you may not be able to issue commands to it with screen -X
for instance. However note, that you can pass commands using escape sequences (though it's not enabled by default for security reasons).
I find that while screen doesn't interpret named characters such as \n
, it does take care of octal escapes. So instead of stuff 'echo "The array has of elements."\n'
, you could use:
stuff 'echo "The array has of elements."'\012
Best Answer
I recently had to do this. On Stack Overflow I answered how to find the PID of the process running in screen. Once you have the PID you can use
ps
to get the command. Here is the contents of that answer with some additional content to address your situation:You can get the PID of the screen sessions here like so:
Let us suppose that you want the PID of the program running in Bash in the
foo_Monitor
screen session. Use the PID of thefoo_Monitor
screen session to get the PID of thebash
session running in it by searching PPIDs (Parent PID) for the known PID:Now get just the PID of the
bash
session:Now we want the process with that PID. Just nest the commands, and this time use the
-v
flag ongrep bash
to get the process that is not bash:We can use that PID to find the command (Look at the end of the second line):
Put it all together: