?
Solved

Detecting a running program

Posted on 2005-04-26
12
Medium Priority
?
304 Views
Last Modified: 2010-04-02
Hi,

I would like to know how can I write a small program to detect whether a particular daemon or a specific program is running or not?  What kinda api I can use to know whether the program is running or not???


Xenia
0
Comment
Question by:xenia27
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 400 total points
ID: 13872717
libprocps

See if your distribution provides a package  such as procps-devel
and check out the headers like...
  /usr/include/procps/readproc.h

in any case you will need to be able to decide if a particular process
is the one you're looking for or not

A simpler method would probably be to invoke the shell for this

i.e.  invoke

/bin/sh -c pgrep progName
0
 

Author Comment

by:xenia27
ID: 13872811
Is there other way I can write a small program to do so?  I try to detect a program which is running in "BusyBox"...I cannot see "pgrep" is provided in my unix.  Is there any reference or web site I can study to write this program???


Xenia
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 400 total points
ID: 13873051
ps -aux | grep "program name"

If you wish to do it programatically, you will have to query the proc filesystem ... Ofcourse this will work only on Linux ... Alternatively you can launch the above command from your program using popen and parse the results ...

If you are interested in some specific daemon program, then most (all?) of them create a pid file. You can read that pid file.

Yet another way might be to try and launch the daemon. Most daemons have a mechanism (mostly PID files) to prevent miltiple instances. If daemon is already running, new launch will fail.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:xenia27
ID: 13873383
when my "BusyBox" start running, it will call one of my programs so the program will run like a daemon...which is what I intend to do...and now I need to make sure the program is running; otherwise, I need to call the program again.  So how can I write this program?  How can I read pid files???
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13873445
pid file is a normal text file whose contents are the PID of the running daemon. The file is deleted by the daemon on exit. When starting up, daemon checks for presence of the file. If file is present, a daemon is already running and new instance exits gracefully. Just to be sure that daemon did not crash before deleting the file, you can verify that PID in the file is indeed of the daemon process (query proc or run ps|grep).

You will find this discussion useful
http://www.experts-exchange.com/Programming/Programming_Platforms/Linux_Programming/Q_21298714.html
0
 
LVL 4

Accepted Solution

by:
smpoojary earned 800 total points
ID: 13873572
Consider your daemon name as "TcpServer". Then C-code like
char line[100];
system(" ps -ef | grep \"TcpServer\" > file.log");
fp=fopen("file.log","r");
fgets(line,100,fp);
if(strcmp(line,"") == 0)
    printf("\nmy daemon not running\n");
else
    printf("\nmy daemon running\n");
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 400 total points
ID: 13874705
All the examples that use "ps" have a flaw-- any program can put up "TcpServer" as its process name.  And you're likely to trip up on the ps line "grep TcpServer", which will always show up.

The PID file idea will almost work-- except if two processes start up at nearly the same time and they both try reading/writing the PID file.

A somewhat safer way is to have the program rename its executable when it starts up amd rename it back when its done.
With care.

0
 

Author Comment

by:xenia27
ID: 13882164
I just PID file idea but it won't work in "BusyBox"...at least not working in my unix...
Right now, I'm trying on "ps" idea...
0
 
LVL 4

Expert Comment

by:smpoojary
ID: 13882863
If you are using ps idea, you are better to give daemon file name as some number like 6458123189    Otherwise make sure in command line that  not two process with same daemon name is working.

PID file idea
--------------
In checking program(daemon running or not) use following code

//check.c
    FILE *fp;
    if((fp = fopen("/var/run/myProgram.pid","r")) == NULL)
    {
              printf("\nError : Unable to open file /var/run/myProgram.pid");
              exit(1);
    }
     fscanf(fp,"%d",&pid);
      if (kill(pid, SIGUSR2) == 0)
      {
         fclose(fp);
         // process with pid is still alive, and I have just signalled it to order front
          exit(0);
     }
      else
      {
           fclose(fp);
           remove("/var/run/myProgram.pid");   //No need to remove
           system("./daemon");              //Run daemon process
      }


Inside daemon program code put (in the beginning of the code)
------------------------------------------------------------------------
//daemon.c
signal(SIGUSR2,SIG_IGN);
and also write following code
fp = fopen("/var/run/myProgram.pid","w");
pid = getpid();
fprintf(fp,"%d",pid);
close(fp);
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13883168
Hi Greg,

>The PID file idea will almost work-- except if two processes start up at nearly the same time and they both try reading/writing
>the PID file
File can be opened with O_EXCL flag.
0
 

Author Comment

by:xenia27
ID: 13883209
> If you are using ps idea, you are better to give daemon file name as some number like 6458123189
> Otherwise make sure in command line that  not two process with same daemon name is working.
Not sure I understand what you mean...could you please explain?
0
 
LVL 4

Expert Comment

by:smpoojary
ID: 13883326
Consider your daemon name as "TcpServer". Run the following commands
#./TcpServer
#ps -ef | grep "TcpServer"

Check whether two or more lines are displayed ( Means whether two or more different pid's are exists. Means two or more processes are running with same daemon name). If only one line is there then you can use that name only (here "TcpServer");
-Mahesh
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question