4

Though similar questions asked before in this forum here , here and here, my question is different. I want to get PID of a running bash process in gnome terminal.

Note: Suggested link which posted by moderators doesn't provide any working solution. I don't understand people ignorantly vote for closing my topic.

Commands like

 xprop | awk '/PID/ {print $3}' | xargs ps h -o pid,cmd
 xprop _NET_WM_PID | cut -d' ' -f3

give me only gnome-terminal parent PID not bash process ID

If I run

 PID=$$; echo $PID

or

 cut -d ' ' -f 4 /proc/self/stat

gives me exact PID of terminal but I have to run those commands in terminal to get the PID.

I have a script that toggles process of a terminal between kill -STOP PID and kill -CONT PID with a shortcut key combination.

    #!/bin/bash
    myid=$(ps $(xdotool getwindowfocus getwindowpid) |tail -n 1 |awk '{print $3}')
    if [[ $myid == "Sl" ]]
    then
    kill -STOP `xdotool getwindowfocus getwindowpid`
    else
    kill -CONT `xdotool getwindowfocus getwindowpid`
    fi

As I mentioned the line myid=$(ps $(xdotool getwindowfocus getwindowpid) |tail -n 1 |awk '{print $3}')

returns only PID of parent gnome-terminal

Is there a way to achieve what I want? `

EDIT: Beside Dmitry Alexandrov's answer, I also found another working solution here How to know the pid of active window

EDIT2: I spoke too early. I don't know why but above solution is not working reliably. So I am using Dmitry Alexandrov's solution, it works well except a glitch in my script.

   #!/bin/bash
   read __ __ TERM_PID < <(xprop _NET_WM_PID) &&\
   SH_PID=$(ps --ppid "$TERM_PID" -o pid=)
   mypid=$(echo $SH_PID | awk  ' { print $2 } ' )
   myid=$(ps $mypid |tail -n 1 |awk '{print $3}')
   if [[ $myid == "Sl" ]]
   then
   kill -STOP $mypid
   else
   kill -CONT $mypid
   fi
Note: Process STAT is not reliable, it doesn't always toggle between Sl and T, it sometimes takes different states such as `Ss+` `S<` 

EDIT3:

This one works well

   #!/bin/bash
   read __ __ TERM_PID < <(xprop _NET_WM_PID) &&\
   SH_PID=$(ps --ppid "$TERM_PID" -o pid=)
   mypid=$(echo $SH_PID | awk  ' { print $2 } ' )
   myid=$(ps $mypid |tail -n 1 |awk '{print $3}')
   if [[ *$myid* == *"S"* ]]
   then
   kill -STOP $mypid
   else
   kill -CONT $mypid
   fi
kenn
  • 5,162

1 Answers1

2

If terminal has only one shell process, I cannot see a problem.

read __ __ TERM_PID < <(xprop _NET_WM_PID) &&\
SH_PID=$(ps --ppid "$TERM_PID" -o pid=)
kill -STOP "$SH_PID"

Works for XTerm, should work for GNOME Terminal too, I believe.

Otherwise – if there may be more than one shell running under single terminal process (in several windows, tabs, regions, via multiplexer, etc), it’s not clear from the question, what do you want.

Dmitry Alexandrov
  • 1,578
  • 10
  • 12
  • Thank you. Your answer close to a solution. I am confused that $SH_PID returns two PIDS like 5535 5536 the latter is exact PID of the bash process. Though I haven't figured out how it works, I think I can handle the rest – kenn Sep 01 '14 at 11:05