Closing a graphical application by pid

Hi all,

I have a small graphical application whose only purpose is to pop up certain types of messages. Currently, I'm using it in a Bash script like this:

./MyProg "this is my message"
while [ something ]
do
   ... do things ...
done

What I want to do is after the while loop is over (it does break out eventually, no infinite loop there), end MyProg so the message disappears.

I tried to do that by killing the process ID, like this:

./MyProg "this is my message"
pid=$!
while [ something ]
do
   ... do things ...
done
kill -s 15 $pid

But the pid was empty. I then tried to run MyProg in the background
:

./MyProg "this is my message" &
pid=$!
while [ something ]
do
   ... do things ...
done
kill -s 15 $pid

And got a pid, but then the kill command said that the pid didn't exist.

Is there something else I'm missing here, or a better way to do this besides kill?

Thanks,
Zel2008

If MyProg forks itself into the background, you won't be able to capture it's PID. This seems to be the case. Perhaps the documentation of MyProg can shed some light on it, or it's behavior can be changed to stay in the foreground, in which case you'd be able to use your second approach.

Thanks neutronscott,

I can only get a pid when MyProg is in the background, that's the weird thing. If the process is in the foreground, the process id is empty. I wrote MyProg myself, and it doesn't have anything in it that would make it preferentially do anything in the background or foreground.

Thanks,
Zel2008

The definition of $! is "Expands to the process ID of the most recently executed background (asynchronous) command."

But since I assume your while loop runs after using MyProg without & , then MyProg must be forking itself into the background and therefore the shell hasn't a way to know the PID anyway, even if it backgrounds MyProg.

./MyProg & pid=$!

$pid may end up being 1234, but MyProg does a fork() and continues running at 1238 ... How would we know that number?

1 Like

Thank you neutronscott,

I didn't know that about pid, now I think I know how to fix this. :slight_smile: