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.
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.
...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 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.
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.
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.
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.
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.
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.
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.
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.
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. Come to think of it, there are a ton of flags in the usage output while aren't displayed in the man page.
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.
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?
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.
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.
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".