If I run these commands:
...
geany --new-instance --no-msgwin --no-session --no-terminal -c pathtoconfig/ &
pid=$!
echo $pid
wmctrl -lp
read -p "waiting..."
...
the resulting PID does not match that of the geany
process launched. But if a sleep 1
command is inserted before reading the PID (after the geany
line), then the PID is correct. The reason for this is that windowing takes some time, so wmctrl -lp
provides preliminary information which does not much precisely the right PID.
What would be the best way to wait for geany to complete launching before the new window has stabilized and wmctrl
is aware of it?
Update: - Run on Lubuntu 16.04 / HP ProBook 6360b.
Actual PID for comparison was obtained with
wmctrl -lp
that includes PID in the list.PID is fetched correctly by
$!
, what takes some time to update is the one provided bywmctrl -lp
, that is different after some time (some 0.27s forgeany
, some 0.16s forleafpad
), as if the window manager would take some time to update the PID.
geany
this two ways:geany &
andgeany --new-instance --no-msgwin --no-session --no-terminal &
. Did the problem remained? – MiniMax Jul 23 '17 at 15:18geany &
(it does get the correct PID). Then tried several alternatives, removing some options (doesn't get the correct PID), and finally the simplified one again (and now, does NOT get the correct PID). This was done isolating the piece of code, to make sure that it only relies on launchinggeany
. – nightcod3r Jul 23 '17 at 22:12geany
launching time, does not matter because of how fork–exec works. And another helpful answer about&
operator: process in the background. By the way, how you get second PID for comparing? – MiniMax Jul 23 '17 at 23:04geany &
the PID after settling is 11078,$!
got 11118, withleafpad
was 11078, and$!
got 11173. In both cases, the PID matches perfectly whensleep 1
is added. So, in principle it doesn't seem to be related to launching time, but waiting for some time actually helps. – nightcod3r Jul 24 '17 at 07:33geany &
will work fine for a delay of 0.27s, whileleafpad &
does work for delays over 0.16s. Something is going on after the program is launched. (Technical data added to the question.) – nightcod3r Jul 24 '17 at 07:46wmctrl
, as$!
seems to work just fine. – nightcod3r Jul 24 '17 at 08:09wmctrl
, I just was doinggeany & pid=$!; echo $pid
, then was runningps
manually and checkinggeany
's actual PID. I thought, thatwmctrl -lp read -p "waiting..."
was added for context only. :) – MiniMax Jul 24 '17 at 09:07wmctrl -lp
list after 3 seconds it was launched. And this is logical, because this list shows "windows being managed by the window manager". And running program's binary file and appearing this program in the window, happens at the not same time. Hence, we have a delay, between these two events. Now, the question is: how catch this event, right? – MiniMax Jul 24 '17 at 13:33wmctrl -lp
until the window shows up doesn't look like an elegant solution. Any hint in a different direction? (Title has been updated.) – nightcod3r Jul 24 '17 at 13:41sleep 0.1
in to the loop checking. It will check 10 times per second. For reduce the impact to the PC performance. – MiniMax Jul 24 '17 at 14:44