1

How can I run a command that lists all PIDs of the processes running under a certain screen (suppose multiple bash processes are running, because I have opened multiple tabs)?. I found the following post: Find out which command is running within a screen session

That command works when there is only one process running within a screen, but it fails when I have more than one tab open in the screen session. The error I get is as follows:

command:

ps u -p $(ps -el | grep $(ps -el | grep 28572 | grep bash | awk '{print $4}') | grep -v bash | awk '{print $4}')

output:

grep: 28573: No such file or directory

UPDATE:

The following sequences of commands yield the expected results.

  1. Get all screen processes

    ps waux | grep -i screen | grep -v grep | awk '{print $2}' | paste -s -d,
    
  2. Get all the processes running under the screen sessions

    pgrep -P $(ps waux | grep -i screen | grep -v grep | awk '{print $2}' | paste -s -d,) | paste -s -d,
    
  3. Get all the java processes running under the processes running under the screen sessions

    ps -w -p $(pgrep -P $(pgrep -P $(ps waux | grep -i screen | grep -v grep | awk '{print $2}' | paste -s -d,) | paste -s -d,) | paste -s -d,) | grep java
    

Things to note:

  1. I only care about java processes running within the screen sessions.
  2. I am using the paste command to get a comma-separated list of process identifiers, which I then use as the parameter of the -P option of the pgrep command.

UPDATE 2:

I found a way of avoiding using the paste command by using xargs:

ps waux | grep -i screen | grep -v grep | awk '{print $2}' | xargs -I{} pgrep -P {} | xargs -I{} pgrep -P {} | xargs -I{} ps -w -p {} |  grep java
2
  • 1
    try using the pstree command Jan 2, 2019 at 21:00
  • @RuiFRibeiro so if I run the pstree command I get the following: pstree 28572 screen─┬─bash └─bash───java───30*[{java}] . How can I get the pid of the java process? Jan 2, 2019 at 21:07

1 Answer 1

0

Try this, it would only work if there was one screen session open, though.

ps waux | grep screen$ | awk '{print $2}' | xargs pstree

For example

jeff@linux:~$ ps waux | grep screen$ | awk '{print $2}' | xargs pstree
screen───screen─┬─bash───top
                ├─bash───watch
                └─bash───xargs───pstree

If you had multiple screen sessions, you could iterate over them like this:

jeff@jlinux:~$ for pid in $(ps waux | grep screen$ | awk '{print $2}'); do pstree $pid; done
screen───screen───bash
screen───screen─┬─bash───top
                ├─bash───watch
                └─bash───pstree
5
  • Thank you Jeff, but I actually want to get the PIDs of top, watch and pstree in this particular case. I will update the title of my question. Jan 3, 2019 at 0:26
  • @MiltonInostrozaAguilera just add the "-p" flag to pstree
    – Jeff A
    Jan 3, 2019 at 0:27
  • for some reason, the command for multiple screen session come up empty. I verified that I have more than one screen session by running screen -ls: There are screens on: 31136.test1 (Detached) 12989.test2 (Detached) 28572.test3 (Detached) However, when I run your command the result is empty. Jan 3, 2019 at 0:50
  • sorry for the non formatted text, but I tried formatting and it didn't quite work out :S. Is it possible to just the list of PIDs rather than the grapichal tree? Jan 3, 2019 at 0:57
  • I think I got what I was looking for. Updating my initial question. Jan 4, 2019 at 0:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .