Closing a graphical application by pid


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Closing a graphical application by pid
# 1  
Old 07-14-2014
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:

Code:
./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:
Code:
./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
:
Code:
./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
# 2  
Old 07-14-2014
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.
# 3  
Old 07-14-2014
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
# 4  
Old 07-14-2014
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?
This User Gave Thanks to neutronscott For This Post:
# 5  
Old 07-14-2014
Thank you neutronscott,

I didn't know that about pid, now I think I know how to fix this. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cannot get terminal application to launch with a graphical launcher when successful in terminal

I have been having an extremely annoying problem. For the record, I am relatively new at this. I've only been working with unix-based OS's for roughly two years, mostly Xubuntu and some Kali. I am pretty familiar with the BASH language, as that's the default shell for debian. Now, I've made this... (16 Replies)
Discussion started by: Huitzilopochtli
16 Replies

2. Solaris

Porting graphical Solaris application to Linux

I don't expect any quick answers, but if people have links to resources I can investigate I'd be extremely appreciative. Here is what we have today: The "application" is a multi-process train control system that uses the Unix desktop, currently CDE, several motif-based applications and sound,... (8 Replies)
Discussion started by: paz9
8 Replies

3. Shell Programming and Scripting

Closing open file descriptors from /proc/pid/fd

Hi guys, i need to write a shell script that will close file descriptors from /proc/pid/fd will calling exec 4<&- solve the problem ? thanks in advance :) (15 Replies)
Discussion started by: alpha_romeo
15 Replies

4. UNIX and Linux Applications

invoke remote graphical application..and display locally

Hi, I want to invoke(run) a graphical application remotely, and the display should be in remote itself. (no X redirect).i want to do this through ssh. like if i login to a remote machine and run firefox it should display there itself. how can i do this..? (2 Replies)
Discussion started by: madhusudankh
2 Replies

5. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

6. UNIX for Dummies Questions & Answers

graphical usage

Hi, is there any graphical tool for to see the solaris unix resource usages like cpu usage,memory,disk i.o..etc? thanks (0 Replies)
Discussion started by: rags_s11
0 Replies

7. AIX

How to prevent an application from closing a file

I'm writing some software tests, & one of my test cases is to prevent an address space from closing a data file (file is closed & a new one opened every 15 minutes). I can't remove or rename the file while it's being written to, any other ideas to prevent a file from being closed - or at least... (4 Replies)
Discussion started by: jasahl
4 Replies

8. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies

9. Programming

printing ppid,child pid,pid

question: for the below program i just printed the value for pid, child pid and parent pid why does it give me 6 values? i assume ppid is 28086 but can't figure out why there are 5 values printed instead of just two! can someone comment on that! #include<stdio.h> #define DIM 8 int... (3 Replies)
Discussion started by: a25khan
3 Replies

10. IP Networking

Closing out ports???

Hi all Is there a command that I can use to close out open ports? I did a netstat - a -p and got a long list of ports open (see sample below). I have disabled the some of the applications from /etc/services/. But there are still applications listening on certain ports. I need to know how to... (6 Replies)
Discussion started by: skotapal
6 Replies
Login or Register to Ask a Question