0

I am executing a file as a daemon process. How can I stop the daemon?

int main(int argc, char **argv)
{
    if ( argc != 5 )
    {
        printf ("Usage: %s <server> <nick> <channel> <logging>\n", argv[0]);
        return 1;
    }       
    char *startPath =  malloc(sizeof(char) *100);

strcpy(startPath,"/home/.../start");    

int child_pnr;

if(daemonisieren() != 0) 
{
    printf("damonization not possible");
    exit(0);
}   

printf("I am a damon \n");

 if((child_pnr = fork())==0)
{       
    execve(startPath,argv); 
}
else if (child_pnr > 0)
{   
    printf("I am parent and have a child \n");
    wait(child_pnr);
 }

printf("gone....\n");

free(startPath);
}

I presume I can kill it just like kill(childnr) but as the parent process waits for the child to finish execution which he may never do I have to have a program which knows the childnr and kills it. How can I do that?

Wooble
  • 87,717
  • 12
  • 108
  • 131
Fendrix
  • 556
  • 2
  • 11
  • 34

1 Answers1

1

You can make the child's process number available using many different methods, all of which can be effective. A simple way is to just store it into a file on your system. If you look in /var/run you will probably find many XXX.pid files doing that already.

Perhaps a better solution for your use case though is to start a new thread in your parent (or have it fork a second child) which performs a sleep for some amount of time and if that time elapses, it kills the primary child. If the primary child terminates on its own, satisfying the parent's wait, the parent can kill the "watchdog" child (or thread).

mah
  • 39,056
  • 9
  • 76
  • 93
  • thanks... second method is nice to have but the programm may run for some hours ... don't know how long... the first one is ok, but if I want to read in /var/run I need to have root which my application may not have... – Fendrix Sep 20 '12 at 17:26
  • The location /var/run is convenient (and appropriate) if you can write there of course, but there's nothing magic about the location... anyplace you have write access should be fine. As to a watchdog and running for hours, a thread (`man pthread_create` for initial details) would be very lightweight. – mah Sep 20 '12 at 17:30
  • @Fendrix, what's the criteria you have for when you want to kill the child process ? If you send a signal to your parent process, wait will return, with an error and set errno to EINTR. So you could use that fact and kill() the child process if the parent receives a signal. – nos Sep 20 '12 at 17:30
  • Ok.. I think the application which is getting started by the child is similar to a server... it means it runs hours,days... for e.g. apache... u can stop him like xxxx -stop ... I would like to do the same in my case – Fendrix Sep 20 '12 at 17:37
  • 1
    With this new description I would recommend your child create a posix named semaphore (http://linux.die.net/man/7/sem_overview -- see `sem_open` to create it) and have a thread that blocks waiting for that semaphore to be posted. When another process (the same binary, taking action based on the command line) posts the semaphore, the thread will unblock and do whatever it takes for an orderly shutdown of your application. – mah Sep 20 '12 at 17:47