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
  • Have you given the solutions in this Q&A a try? – Glutanimate Aug 31 '14 at 19:51
  • @Glutanimate yes, I tried that example too. It returns gnome-terminal- title only, it's parent process of all child bash processes – kenn Aug 31 '14 at 20:27
  • @Glutanimate wininfo also doesn't discriminate child PIDs in gnome-terminal. It only discriminates active windows but it doesn't print their own PIDs – kenn Aug 31 '14 at 20:39
  • possible duplicate of Which window has current focus? – Panther Aug 31 '14 at 20:48
  • The bash console uses a pseudo terminal handled in the background by the Gnome (or other) terminal. I don't think you'll be able to get access to that data easily... – Alexis Wilke Aug 31 '14 at 21:20
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.

| improve this answer | |
  • 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 1 '14 at 11:05

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.