Get PID of process launched from bash script

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
SemiSolidState
Level 1
Level 1
Posts: 21
Joined: Wed Dec 09, 2020 11:59 am

Get PID of process launched from bash script

Post by SemiSolidState »

Sometimes I run a bash script that launches multiple Firefox windows and then sizes and positions them on certain monitors. An example below shows how one Firefox is launched and manipulated based on the name of whatever website is currently loaded. I don't like that because I have to add a delay to make sure the site has time to load and there is too much room for error. I can't manipulate Firefox in general because I may have any number of other Firefox windows open on various monitors.

Code: Select all

nohup firefox google.com &> /dev/null &
sleep 5s
wmctrl -r Google -b remove,maximized_vert,maximized_horz
xdotool search --name "Google" windowmove 3640 2160 windowsize 1719 1375
Within the bash script I've been trying to figure out how to capture the PID of each Firefox instance as it launches them. I figure feeding wmctrl and xdotool the PID's will be more reliable and help me avoid or reduce the sleep delays.

PID=$! immediately after the line that launches Firefox doesn't work from within the script. The PID it returns is not that of the new Firefox instance. I've searched and tried a variety of options from the web but haven't been able to find the solution and could use some help.

Thank you.
Last edited by LockBot on Thu Jul 13, 2023 10:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Get PID of process launched from bash script

Post by Termy »

Unfortunately, every Firefox instance seems to have a lot of PIDs. My current Firefox instance has a bit over 11 PIDs associated with it.

However, when I just now ran, ...

Code: Select all

for PID in `pidof firefox`; { ps -p $PID; }
...I was able to determine that only one of the 11 processes was classified as Firefox. I suspect this one PID is what you want and will need to programmatically determine in a similar way.
I'm also Terminalforlife on GitHub.
SemiSolidState
Level 1
Level 1
Posts: 21
Joined: Wed Dec 09, 2020 11:59 am

Re: Get PID of process launched from bash script

Post by SemiSolidState »

I think maybe all the Firefox windows are part of one or more shared Firefox PID's so maybe I need to figure out how to capture the Window ID of the newly launched one.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Get PID of process launched from bash script

Post by Termy »

I just now decided to watch -n 0.1 -t pidof firefox, which showed the expected 11 PIDs. I have two tabs open, one for GitHub and the other for this thread. When I opened a new and empty tab, no new PIDs were established. When I browsed to a website within that new tab, a new PID appeared on the list. I was able to predictably recreate this effect. I can therefore surmise that each tab in Firefox is a separate process. Just an interesting observation which might somehow help.

As for grabbing the PID of the last opened window. Short of there being a proper way to do this, I might have the script initially grab and remember the current list of Firefox PIDs, then have the script simply act when it detects a PID other than those in the list. It's not concrete, because anything could happen in the interim, and it's especially problematic if it's a system used by multiple users, but it's something.

Actually, you could also grab the title of the Firefox window, which should change depending on the active tab. I sometimes use this fact to have my window manager (i3WM) act according to the active page. You could use xwininfo(1) or xprop(1) to grab this information, but I'm not 100% sure of how to do this in a sane way automatically, short of giving either one the window ID. How easy this stuff is, often depends on the window manager. I think the above PID idea might be easiest, albeit rudimentary. If your window manager provides decent information, you could use that, but it could get more complicated than you'd like.

Food for thought, at least.

There may even be a flag or two for the window manager you're using which has it output plenty of useful information about the open windows, from which you could determine the desired Firefox window. Determine the window manager, determine the primary command for it, then view its man page to check.
Last edited by Termy on Fri Jan 13, 2023 10:03 am, edited 1 time in total.
I'm also Terminalforlife on GitHub.
SemiSolidState
Level 1
Level 1
Posts: 21
Joined: Wed Dec 09, 2020 11:59 am

Re: Get PID of process launched from bash script

Post by SemiSolidState »

Funny, that's exactly where I was headed next.. getting current Firefox PID's to an array before launching so I can determine which the new one is after.
Thanks for your help.
vimes666
Level 6
Level 6
Posts: 1237
Joined: Tue Jan 19, 2016 6:08 pm

Re: Get PID of process launched from bash script

Post by vimes666 »

This should give you the last firefox started:

Code: Select all

ps -ae | sort -r | grep -im 1 firefox
To put it in a variable:

Code: Select all

lastffpid=$(ps -ae | sort -r | grep -im 1 firefox)
If you think the issue is solved, edit your original post and add the word solved to the title.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Get PID of process launched from bash script

Post by Termy »

Are you a programmer at all? If you have any high level programming experience in something like Python or Perl, you might want to look for a library for an API for the window manager you're using. This might then give you much more control and make your life easier. For example, I use a simple Perl library for i3WM in a couple of my programs which spit out some concise information on i3WM windows and workspaces.
vimes666 wrote: Fri Jan 13, 2023 10:07 am [...]
Fun fact: ps(1) can sort its own output in a flexible manner with the --sort flag.
I'm also Terminalforlife on GitHub.
vimes666
Level 6
Level 6
Posts: 1237
Joined: Tue Jan 19, 2016 6:08 pm

Re: Get PID of process launched from bash script

Post by vimes666 »

Termy wrote: Fri Jan 13, 2023 10:10 am Fun fact: ps(1) can sort its own output in a flexible manner with the --sort flag.
Never too old to learn :)

@SemiSolidState
Do you really need the nohup?
Without it the $! would have worked fine.
If you think the issue is solved, edit your original post and add the word solved to the title.
SemiSolidState
Level 1
Level 1
Posts: 21
Joined: Wed Dec 09, 2020 11:59 am

Re: Get PID of process launched from bash script

Post by SemiSolidState »

I use nohup so the programs stay running after the script completes and terminal closes. I launch the script from a desktop shortcut.
SemiSolidState
Level 1
Level 1
Posts: 21
Joined: Wed Dec 09, 2020 11:59 am

Re: Get PID of process launched from bash script

Post by SemiSolidState »

vimes666 wrote: Fri Jan 13, 2023 10:07 am This should give you the last firefox started:

Code: Select all

ps -ae | sort -r | grep -im 1 firefox
This returns the following.

PID: 4011 ? 00:22:36 firefox-bin

Same 4011 no matter how many times I run it while leaving every new spawned Firefox open. And if I close the last Firefox window that was launched process 4011 still is running so I think it's not reporting the new instance.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Get PID of process launched from bash script

Post by Termy »

Ah-hah! I've just learned something very interesting, which even works on my setup. Run xwininfo -root -children. For me, it shows a complete list of open windows, from which you should be able to (programmatically) determine the desired window. It seems to include the window IDs, which is very helpful. It seems like this is a fairly standard X thing. Adding this one to my notes.
I'm also Terminalforlife on GitHub.
SemiSolidState
Level 1
Level 1
Posts: 21
Joined: Wed Dec 09, 2020 11:59 am

Re: Get PID of process launched from bash script

Post by SemiSolidState »

Yeah, it seems to list all the tabs individually too... I'll have to spend some time seeing what I can do with this information. It sounds useful. Thanks.
vimes666
Level 6
Level 6
Posts: 1237
Joined: Tue Jan 19, 2016 6:08 pm

Re: Get PID of process launched from bash script

Post by vimes666 »

Well I have to admit I haven't used firefox a long time.
I just tried to re-enact it but no matter how hard I tried, I only got one firefox instance that got replaced by a new one every time I ran it.
So there is no way to solve it via pid's. However when I use the --browser option in the firefox command it worked to my expectation, but instead of tabs you will get a seperate window for all the pages you open.

Example:

Code: Select all

#!/bin/bash

firefox google.com --browser &> /dev/null &
echo "process $!"
disown $!

firefox google.com --browser &> /dev/null &
echo "process $!"
disown $!

which gives 2 firefox instances that should be seperately addressable by pid:

Code: Select all

vimes@terry-deb:~$ tezt3
process 85044
process 85045
If you think the issue is solved, edit your original post and add the word solved to the title.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Get PID of process launched from bash script

Post by Termy »

vimes666 wrote: Fri Jan 13, 2023 11:09 am [...]
Ooo, nice. Weird that it's not listed in my Firefox man page, but it is in the usage output (firefox --help). Guessing they just haven't updated the man page in ages. :roll: Come to think of it, there are a ton of flags in the usage output while aren't displayed in the man page.
I'm also Terminalforlife on GitHub.
SemiSolidState
Level 1
Level 1
Posts: 21
Joined: Wed Dec 09, 2020 11:59 am

Re: Get PID of process launched from bash script

Post by SemiSolidState »

Termy wrote: Fri Jan 13, 2023 10:47 am Ah-hah! I've just learned something very interesting, which even works on my setup. Run xwininfo -root -children. For me, it shows a complete list of open windows, from which you should be able to (programmatically) determine the desired window. It seems to include the window IDs, which is very helpful. It seems like this is a fairly standard X thing. Adding this one to my notes.
I had my script run xwininfo -root -children | grep firefox before and after launching a new Firefox window then compare the output. There was no difference.
SemiSolidState
Level 1
Level 1
Posts: 21
Joined: Wed Dec 09, 2020 11:59 am

Re: Get PID of process launched from bash script

Post by SemiSolidState »

vimes666 wrote: Fri Jan 13, 2023 11:09 am ...So there is no way to solve it via pid's. However when I use the --browser option in the firefox command it worked to my expectation, but instead of tabs you will get a seperate window for all the pages you open... which gives 2 firefox instances that should be seperately addressable by pid...
Thanks but I expect this is the same situation where it works as long as not using nohup. I haven't tested it but it seems like that would be the case?
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Get PID of process launched from bash script

Post by Termy »

SemiSolidState wrote: Fri Jan 13, 2023 11:24 am [...]
I unfortunately had the same result. :? Not sure why. The best bet seems to be to experiment with the window titles. Maybe a mixture of PIDs and titles. It sounds like this will be more hassle than it's worth, TBH.
I'm also Terminalforlife on GitHub.
SemiSolidState
Level 1
Level 1
Posts: 21
Joined: Wed Dec 09, 2020 11:59 am

Re: Get PID of process launched from bash script

Post by SemiSolidState »

Termy wrote: Fri Jan 13, 2023 11:29 am I unfortunately had the same result. :? Not sure why. The best bet seems to be to experiment with the window titles. Maybe a mixture of PIDs and titles. It sounds like this will be more hassle than it's worth, TBH.
Yep, I'm at the same conclusion. This script just automates my workday morning routine by launching a bunch of stuff, Firefox just being a small part of it. It's pretty reliable and having 10 seconds of delays built in isn't a big deal. The thought that it could be improved just nags at me sometimes. Thanks to everyone for trying.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Get PID of process launched from bash script

Post by rene »

It appears what you in fact want is session save/restore. Xfce has such for example. You'd set things up as you want them and then from Session and Startup | Current Session chose to "Save Session". It would be restored on next login again. MATE no idea, Cinnamon as a GNOME fork no doubt at least can do it somehow, although I don't seem to remember this being front and center anywhere. But that's what you'd look for; "session management".
duracell80
Level 3
Level 3
Posts: 111
Joined: Fri Sep 23, 2022 5:51 pm

Re: Get PID of process launched from bash script

Post by duracell80 »

Try this for a quick shortcut to finding a PID via string input.

Add this to the end of ~/.bashrc

Code: Select all

find-ps() {
    ps aux | grep -i "$1"
}
Now quit the terminal start a new one and do

Code: Select all

find-ps gnome-terminal
What is aux?
https://www.linode.com/docs/guides/use- ... x-shortcut

Or to get just that one pid via cut delim by space and getting the pid field

Code: Select all

find-psone() {
    ps -ae | sort -r | grep -im 1 "$1" | cut -d ' ' -f2
}
Then call that in the terminal

Code: Select all

find-psone firefox
Outputs for me:
394537
Locked

Return to “Scripts & Bash”