[Dev] Re: simple daemon dies

John F Davis dev@trilug.org
Wed, 29 May 2002 14:12:57 -0400


Hello

I thought this would be interesting to those who followed the "Re: [Dev]
HOWTO write a daemon" thread.

JD
---------------------- Forwarded by John F Davis/Raleigh/IBM on 05/29/2002
02:10 PM ---------------------------

Glynn Clements <glynn.clements@virgin.net>@vger.kernel.org on 05/29/2002
05:29:36 AM

Sent by:    linux-c-programming-owner@vger.kernel.org


To:    Sindunata <software@dygsp.com>
cc:    linux-c-programming@vger.kernel.org
Subject:    Re: simple daemon dies




Sindunata wrote:

> I'm trying to write a simple daemon background process.
> It connects to PostgreSQL and wait for a notify event and upon receiving
> some event will invoke some URL using curl library.
>
> I got it working already if I run it in foreground. But if I put the
> process in background (using &), and then after sometime the process
> will die.

In which case, the shell will tell you why it died.

Exited normally with zero status:

 [1]+  Done                    prog

Exited normally with non-zero status:

 [1]+  Exit 1                  prog

Terminated due to SIGTERM:

 [1]+  Terminated              prog

Terminated due to SIGIOT (aka SIGABRT):

 [1]+  Aborted                 (core dumped) prog

Terminated due to SIGKILL:

 [1]+  Killed                  prog

... and so on.

> I think there's some signal that I need to catch, can
> someone please help me?

You don't necessarily *need* to catch any signals. Most of the signals
which terminate a process (e.g. SIGHUP, SIGTERM, SIGINT) are *meant*
to terminate the process. When a program catches one of these signals,
it's normally so that it can "clean up" before it terminates, not to
prevent termination altogether.

> Currently i'm trapping the following signals:

>   sigaction (SIGTERM, &act, NULL);
>   sigaction (SIGINT, &act, NULL);

These are meant to kill the process; there's no reason to catch them.

>   /* ignore SIGHUP  & SIGTTOU */
>   act.sa_handler = SIG_IGN;
>   sigaction(SIGHUP, &act, NULL);

Same here.

>   sigaction(SIGTTOU, &act, NULL);

This will only stop the process, not terminate it.

If you're trying to write a daemon (as opposed to simply running in
the background), the process should be entirely disassociating itself
from the terminal and from the process group, so it shouldn't be
affected by terminal-related signals.

--
Glynn Clements <glynn.clements@virgin.net>
-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html