LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2004, 09:14 AM   #1
nefch
LQ Newbie
 
Registered: Feb 2004
Location: Switzerland
Distribution: Ubuntu 6.10
Posts: 6

Rep: Reputation: 0
Question 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.

Regards
Christof
 
Old 02-17-2004, 09:25 AM   #2
tangle
Senior Member
 
Registered: Apr 2002
Location: Arbovale, WV
Distribution: Slackware
Posts: 1,761

Rep: Reputation: 78
Look in /var/run and see if a .pid file is there for that program. Also you could do a ps x | grep <program name> to see if it is running.
 
Old 02-17-2004, 09:35 AM   #3
nefch
LQ Newbie
 
Registered: Feb 2004
Location: Switzerland
Distribution: Ubuntu 6.10
Posts: 6

Original Poster
Rep: Reputation: 0
I do not know the name of the program. I only know the pid.
 
Old 02-17-2004, 09:42 AM   #4
itsjustme
Senior Member
 
Registered: Mar 2003
Location: Earth
Distribution: Slackware, Ubuntu, Smoothwall
Posts: 1,571

Rep: Reputation: 47
Try:

ps -A
 
Old 02-17-2004, 09:50 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
#!/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().
 
Old 02-17-2004, 09:52 AM   #6
nefch
LQ Newbie
 
Registered: Feb 2004
Location: Switzerland
Distribution: Ubuntu 6.10
Posts: 6

Original Poster
Rep: Reputation: 0
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.

Last edited by nefch; 02-17-2004 at 09:53 AM.
 
Old 02-17-2004, 09:59 AM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
Old 02-17-2004, 10:29 AM   #8
nefch
LQ Newbie
 
Registered: Feb 2004
Location: Switzerland
Distribution: Ubuntu 6.10
Posts: 6

Original Poster
Rep: Reputation: 0
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?

Last edited by nefch; 02-17-2004 at 11:32 AM.
 
Old 02-17-2004, 12:27 PM   #9
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.

Last edited by jtshaw; 02-17-2004 at 12:28 PM.
 
Old 02-17-2004, 03:05 PM   #10
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
check the kill comman. Sounds strange but kill is to send a signal and not only a killing signal.

main()
{
int pid;

/* ... Get the process ID of this program. */

getpid(pid);

/* ... Check to see if this program is active. */

printf("R = %d\n", kill (pid, 0));
}
 
Old 02-18-2004, 02:49 AM   #11
nefch
LQ Newbie
 
Registered: Feb 2004
Location: Switzerland
Distribution: Ubuntu 6.10
Posts: 6

Original Poster
Rep: Reputation: 0
kill or ptrace seem to be the way to go. Thanks for your help!

Christof
 
Old 02-18-2004, 06:36 AM   #12
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
A nicer (cleaner way) is to ask for the process nicelevel:


int prio = getpriority(PRIO_PROCESS, pid);

if( errno ) {
puts("Unable to get nice level for requested process");
}else{
puts("Process alive and kicking");
}

The downside to kill and ptrace is that they realy do change something for the process..
 
Old 02-18-2004, 09:41 AM   #13
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
worldmagic thats the way to do it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cant find program Circuit Monkey Linux - Newbie 3 03-25-2005 04:36 AM
Running a program rj686 Linux - Software 16 10-10-2004 07:19 PM
couldn't find program .....my ar$e munkie_poo Mandriva 3 05-24-2004 01:41 PM
need help running a program boogerman Linux - Software 8 11-24-2003 02:17 PM
Running a new program bananaman Linux - Newbie 4 04-08-2003 04:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration