Skip to contentSkip to conversation

Check if a process exists? (EXTENDING THE QUESTION)

DISCUSSION lucianolnx 3 participants 3 messages Apr 9, 2005 – Apr 11, 2005

lore
Share & tools
lore

lucianolnx

author
#0 lore raw

Today, what are the bests ways to know if a deamon is running without knowing its PID ?

My old technique (used until now), is based on recovering the PID saved by the own process in a special and known location (like a file), almost all times persisted (PERSISTENCE IS A PROBLEM).

Luciano

Quoted 6 lines · Hareesh Nagarajan

Hi,

Can a process check if a given PID exists or not? In other words can a process check if an unrelated process is alive? Is there any system call that does this?

Use kill() with a second argument being zero.

    if (kill(pid, 0) == -1) 
    { 
       /* Process is not there. */ 
    } 
    else 
    { 
       /* Process is alive. */ 
    } 

Holger

#1 lore raw
Quoted 1 line · lucianolnx

Today, what are the bests ways to know if a deamon is running without knowing its PID ?

I'm not sure whether this is `the best way' but you can parse some proc file system entry. Especially /proc/$pid/cmdline and /proc/$pid/status will be useful. And you will have to search all the /proc/$pid entries since you don't know the pid..

For example, they provide following information for clamd.

[baris@rhinox]$ ps aux|grep clamd
clamav     424  0.0  3.1  11520  7856 ?        Ss   01:35   0:03 /usr/sbin/clamd
clamav    1184  0.0  3.1  11520  7856 ?        S    09:38   0:00 /usr/sbin/clamd
baris     1238  0.0  0.2   1688   712 pts/2    S+   09:58   0:00 grep clamd
[baris@rhinox]$ cat /proc/1184/cmdline
/usr/sbin/clamd
[baris@rhinox]$ cat /proc/1184/status
Name:   clamd
State:  S (sleeping)
Tgid:   1184
Pid:    1184
PPid:   424
....
....

I also should add that /proc filesystem is _optional_ in configuration. Highly likely it will exist on a target system but a little possibility of being it unselected still there.

Quoted 3 lines

My old technique (used until now), is based on recovering the PID saved by the own process in a special and known location (like a file), almost all times persisted (PERSISTENCE IS A PROBLEM).

Luciano

Signature · 4 lines
"You have to understand, most of these people are not ready to be
unplugged. And many of them are no inert, so hopelessly dependent
on the system, that they will fight to protect it."
                                                        Morpheus

begin:vcard
fn:M.Baris Demiray
n:Demiray;M.Baris
org:Labris Teknoloji
adr:ODTU;;Teknokent Silikon Blok No:24;Ankara;;06531;Turkiye email;internet:[email protected] title:Yazilim Gelistirme Uzmani tel;work:+903122101490
tel;fax:+903122101492
x-mozilla-html:FALSE url:http://www.labristeknoloji.com
version:2.1
end:vcard

#2 lore raw
In reply to lucianolnx

is the daemon/application name unique? maybe u can use a script that makes use of the top-command. or maybe u can make use of the pstat_getproc() c function.(but im not sure if there's a pstat_getproc func in linux... im using hpux on itanium)

the code below relies on the fact that at any given moment, there is only one instance of the application running... the code is just a simple-search-and-print.
------------------------------------------------------
struct pst_status * psa = NULL;
struct pst_status * prc = NULL;

 struct pst_dynamic  psd;
 long                nproc = 0;
 long                i;

if ( pstat_getdynamic(&psd, sizeof(psd), 1, 0) == -1 )
(void)perror("pstat_getdynamic failed");

nproc = psd.psd_activeprocs;
psa = (struct pst_status *)malloc(nproc * sizeof(struct pst_status));

if ( pstat_getproc(psa, sizeof(struct pst_status), nproc, 0) == -1 )
(void)perror("pstat_getproc failed");

// Report the process info as required
prc = (struct pst_status *)psa;
for (i=0; i < nproc; i++)
{
if ( strcmp( prc->pst_cmd, "APPLICATION NAME" ) == 0 )

   {
      (void)printf("PID IS %d \n", prc->pst_pid);
      break;
    }
    ++prc;

}
------------------------------------------------------

Quoted 32 lines · lucianolnx

Today, what are the bests ways to know if a deamon is running without knowing its PID ?

My old technique (used until now), is based on recovering the PID saved by the own process in a special and known location (like a file), almost all times persisted (PERSISTENCE IS A PROBLEM).

Luciano

Quoted 6 lines · Hareesh Nagarajan

Hi,

Can a process check if a given PID exists or not? In other words can a process check if an unrelated process is alive? Is there any system call that does this?

Use kill() with a second argument being zero.

   if (kill(pid, 0) == -1) 
   { 
      /* Process is not there. */ 
   } 
   else 
   { 
      /* Process is alive. */ 
   } 

Holger