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:
$ screen -ls
There are screens on:
1934.foo_Server (01/25/15 15:26:01) (Detached)
1876.foo_Webserver (01/25/15 15:25:37) (Detached)
1814.foo_Monitor (01/25/15 15:25:13) (Detached)
3 Sockets in /var/run/screen/S-ubuntu.
Let us suppose that you want the PID of the program running in Bash in the foo_Monitor
screen session. Use the PID of the foo_Monitor
screen session to get the PID of the bash
session running in it by searching PPIDs (Parent PID) for the known PID:
$ ps -el | grep 1814 | grep bash
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 1815 1814 0 80 0 - 5520 wait pts/1 00:00:00 bash
Now get just the PID of the bash
session:
$ ps -el | grep 1814 | grep bash | awk '{print $4}'
1815
Now we want the process with that PID. Just nest the commands, and this time use the -v
flag on grep bash
to get the process that is not bash:
$ echo $(ps -el | grep $(ps -el | grep 1814 | grep bash | awk '{print $4}') | grep -v bash | awk '{print $4}')
23869
We can use that PID to find the command (Look at the end of the second line):
$ ps u -p 23869
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
dotanco+ 18345 12.1 20.1 5258484 3307860 ? Sl Feb02 1147:09 /usr/lib/foo
Put it all together:
$ ps u -p $(ps -el | grep $(ps -el | grep SCREEN_SESSION_PID | grep bash | awk '{print $4}') | grep -v bash | awk '{print $4}')