How do I find out if another program is still running?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do I find out if another program is still running?
Hi.
What is the best way to find out if another program is still running? The two programs are unrelated. One program knows the pid of another program and wants to know if that other program is still running.
#!/bin/sh
# $1 = pid of process to find
# you may have to adjust the cut
# paramters to match where the pid is on your display
# eg., ps -A or whatever
ps -ef | cut -c 9-15 | grep -q "$1"
if [ $? -eq o ] ; then
echo "Still running"
else
echo "Not running"
fi
There is one problem - on busy systems another, new process can come along and have the same pid as the one you were looking for. This would give you a false positive.
If you are waiting on the process from inside C, look into wait(), or waitpid().
I need to know how to do this in C/C++. Is there no API to do this, like OpenProcess() in Windows?
I don't want to wait for the other process. I just need to know if it is still around.
OpenProcess() in unix is: fork() exec()
Fork returns the pid to the parent process, zero to the child.
The child runs specific code. The parent skips over it, but can save the pid from the fortk() call.
exec() runs the code. There are different flavors of exec, read the man pages to find the one that meets your needs.
Using the pid still has the problem I outlined earlier, especailly if the parent code runs for a while before checking the child.
This doesn't make sense to me. I do not want to start another program - it is already running. There is no parent/child relationship between these two programs.
If program A knows the pid of program B, how can A check if B is still running?
What would A need to know about B to be able to identify B for sure?
ptrace will let you trace other pids. I realize you don't want to trace it, but you could always attempt to connect to it (it returns an error code if it doesn't exist) and then let go of it if it does exist.
Quote:
Description:
The ptrace system call provides a means by which a parent process may observe and control the execution of another process, and examine and change its core image and registers. It is primarily used to implement breakpoint debugging and system call tracing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.