I have logged in in a server.Then issued the "screen -list" command which gives the following output -

There is a screen on:
11048.pts-0.devops (Detached)
27563.pst-0.devops (Detached)
1 Socket in /var/run/screen/S-root.

Now I have to terminate one of the screen session using "screen -S -X quit". Before terminating I want to know which command is running in the screen.For example I have run './startup.sh' (for tomcat) in screen with id '11048' but I forgot it. Is there any way to find it out which command is run under this screen with id 11048?

Thanks in advance

  • 1
    If you are doing this interactively, why not simply attach and have a look at all the things that are running in each? – Håkan Lindqvist Jul 16 '14 at 22:50

The syntax to open the screen is:

screen -r <pid>

so the first screen would be "screen -r 11048" - to detach the screen again type ctrl+a d (as in hold ctrl while hitting a, then release, then d). Once in screen you can also use ctrl+a " to navigate between screens. Also you should know that if you just kill 11048 the process running in screen 11048 will keep running after screen quits.

---- edit ----

Oh, I see what you mean. Try tracing the child processes:

seqb [~]# ps -ef | grep SCREEN
505       5835     1  0 Jun30 ?        00:00:01 SCREEN
505      19501     1  0 Feb27 ?        00:00:00 SCREEN
505      21852     1  0 Mar02 ?        00:00:07 SCREEN
root     22035 22006  0 19:05 pts/2    00:00:00 grep SCREEN
root     29668     1  0 Jul08 ?        00:00:00 SCREEN
seqb [~]# ps -ef | grep 29668
root     22038 22006  0 19:06 pts/2    00:00:00 grep 29668
root     29668     1  0 Jul08 ?        00:00:00 SCREEN
root     29669 29668  0 Jul08 pts/0    00:00:00 /bin/bash
seqb [~]# ps -ef | grep 29669
root     22047 29669  0 19:08 pts/0    00:00:00 sleep 600
root     22049 22006  0 19:08 pts/2    00:00:00 grep 29669
root     29669 29668  0 Jul08 pts/0    00:00:00 /bin/bash

so when you search for the pid of screen in ps -ef (29668), you'll also get the process that considers SCREEN to be it's parent. That'll usually be bash. So then when you search for the pid of bash, you'll find the process running in bash, in this case "sleep 600". Run ps -ef | head -1 if it doesn't quite make sense.

There's also a shortcut - screen keeps the last 500 lines in memory. Type ctrl+a [ and then the up key. You can go to the top of the output, if there are fewer than 500 lines of output then you can see what command is running.

  • But what should I do if I want to know after long time, which command/process is running under the screen session which I have just opened with 'screen -r <pid>'? Thanks for your response. – Razib Jul 16 '14 at 23:15
  • Added more info ^ – Some Linux Nerd Jul 16 '14 at 23:23
  • 1
    Just to (hopefully) make things less confusing, the screen session has windows, not screens. ("you can also use ctrl+a " to navigate between windows") – Håkan Lindqvist Jul 16 '14 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.