Stopping daemon in linux

classic Classic list List threaded Threaded
23 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Stopping daemon in linux

Wimpie Nortje
Hi

I am writing an application using lazdaemon. How does one stop the
daemon gracefully, ie not killing it.

The wiki says the daemon runs until it receives the TERM signal. THe
only way I know to send the TERM signal is using 'kill'. This kills the
app rather letting it exit gracefully.

Thanks
Wimpie

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Marco van de Voort
In our previous episode, Wimpie Nortje said:
>
> I am writing an application using lazdaemon. How does one stop the
> daemon gracefully, ie not killing it.
>
> The wiki says the daemon runs until it receives the TERM signal. THe
> only way I know to send the TERM signal is using 'kill'. This kills the
> app rather letting it exit gracefully.

Known issue, see

http://bugs.freepascal.org/view.php?id=14741

there are some more signals that are handled.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Wimpie Nortje


Marco van de Voort wrote:
In our previous episode, Wimpie Nortje said:
  
I am writing an application using lazdaemon. How does one stop the 
daemon gracefully, ie not killing it.

The wiki says the daemon runs until it receives the TERM signal. THe 
only way I know to send the TERM signal is using 'kill'. This kills the 
app rather letting it exit gracefully.
    

Known issue, see

http://bugs.freepascal.org/view.php?id=14741

there are some more signals that are handled.
  
Are there plans to fix/change this, or must I implement some kind of pid check myself?

I also see a mention of a  '-b' parameter in the report but it doesn't daemonize my application. Is it new? I am using fpc 2.2.4

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Marc Santhoff-2
In reply to this post by Wimpie Nortje
Am Sonntag, den 08.11.2009, 21:18 +0200 schrieb Wimpie Nortje:
> Hi
>
> I am writing an application using lazdaemon. How does one stop the
> daemon gracefully, ie not killing it.
>
> The wiki says the daemon runs until it receives the TERM signal. THe
> only way I know to send the TERM signal is using 'kill'. This kills the
> app rather letting it exit gracefully.

You can send any defined signal using kill. Try:

# kill -TERM <yourdeamon>

for sending the TERM signal. At least FreeBSDs kill works that way. If
yours doesn't try:

$ man kill

HTH,
--
Marc Santhoff <[hidden email]>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Marco van de Voort
In reply to this post by Wimpie Nortje
In our previous episode, Wimpie Nortje said:

> Marco van de Voort wrote:
> > In our previous episode, Wimpie Nortje said:
> >  
> >> I am writing an application using lazdaemon. How does one stop the
> >> daemon gracefully, ie not killing it.
> >>
> >> The wiki says the daemon runs until it receives the TERM signal. THe
> >> only way I know to send the TERM signal is using 'kill'. This kills the
> >> app rather letting it exit gracefully.
> >>    
> >
> > Known issue, see
> >
> > http://bugs.freepascal.org/view.php?id=14741
> >
> > there are some more signals that are handled.
> >  
> Are there plans to fix/change this, or must I implement some kind of pid
> check myself?

No plans yet. The problem is also a bit that whatever you do is an island. I
think this is more something for freedesktop to standarize first.

> I also see a mention of a  '-b' parameter in the report but it doesn't
> daemonize my application. Is it new? I am using fpc 2.2.4

It's in combination with start only. I just happened to look in those sources
today before your msg because of the above report. I don't know the history.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Wimpie Nortje


Marco van de Voort wrote:


No plans yet. The problem is also a bit that whatever you do is an island. I
think this is more something for freedesktop to standarize first.
  
I know there is a standard/recomended way to write a daemon. If you use KDevelop it will create such a template. I don't know if that includes stopping the daemon but I will have a look there.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Wimpie Nortje
In reply to this post by Marc Santhoff-2


Marc Santhoff wrote:
> You can send any defined signal using kill. Try:
>
> # kill -TERM <yourdeamon>
>
> for sending the TERM signal. At least FreeBSDs kill works that way. If
> yours doesn't try:
>  
That is what I did. This doesn't run my daemon's exit routines. I don't
know if Linux just kills the daemon or if the daemon receives the signal
and exits properly without calling the exit routines.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Michael Van Canneyt


On Mon, 9 Nov 2009, Wimpie Nortje wrote:

>
>
> Marc Santhoff wrote:
>> You can send any defined signal using kill. Try:
>>
>> # kill -TERM <yourdeamon>
>>
>> for sending the TERM signal. At least FreeBSDs kill works that way. If
>> yours doesn't try:
>>
> That is what I did. This doesn't run my daemon's exit routines. I don't know
> if Linux just kills the daemon or if the daemon receives the signal and exits
> properly without calling the exit routines.

It should stop the daemons properly. This is the code that gets executed:

Procedure DoShutDown(Sig : Longint; Info : PSigInfo; Context : PSigContext);
cdecl;

begin
   Application.StopDaemons(True);
   Application.Terminate;
end;

If it doesn't, something is wrong :(

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Marco van de Voort
In reply to this post by Wimpie Nortje
In our previous episode, Wimpie Nortje said:
> > No plans yet. The problem is also a bit that whatever you do is an island. I
> > think this is more something for freedesktop to standarize first.

> I know there is a standard/recomended way to write a daemon. If you use
> KDevelop it will create such a template.

A daemon and starting stopping it are two different things.

> I don't know if that includes
> stopping the daemon but I will have a look there.

To my best knowledge, the scripts in init.d/ etc handle the starting and
stopping and storing PIDs, not the applications themselves.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Wimpie Nortje
In reply to this post by Michael Van Canneyt


Michael Van Canneyt wrote:

> It should stop the daemons properly. This is the code that gets executed:
>
> Procedure DoShutDown(Sig : Longint; Info : PSigInfo; Context :
> PSigContext);
> cdecl;
>
> begin
>   Application.StopDaemons(True);
>   Application.Terminate;
> end;
>
> If it doesn't, something is wrong :(
I think something is wrong. :(

For my TDaemon descendant I assigned the following events:
OnDestroy
OnExecute
OnPause
OnShutdown
OnStart
OnStop

When I type "./daemon -r &"
The following handlers are executed:
1. OnStart
2. OnExecute

When I type "kill -TERM daemon_pid"
The following handler is executed
1. OnDestroy

I am also a bit confused by the wiki. According to the wiki Start and
stop are triggered under linux but shutdown is not. The 'Taming the
daemon' article does not specify which of these 3 are called under linux.

As I mentioned earlier, I use FPC 2.2.4. Could that be the problem?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Jonas Maebe-2
In reply to this post by Michael Van Canneyt

On 09 Nov 2009, at 09:07, Michael Van Canneyt wrote:

> It should stop the daemons properly. This is the code that gets  
> executed:
>
> Procedure DoShutDown(Sig : Longint; Info : PSigInfo; Context :  
> PSigContext);
> cdecl;
>
> begin
>  Application.StopDaemons(True);
>  Application.Terminate;
> end;
>
> If it doesn't, something is wrong :(

Are both of these routines and all code they call safe for  
asynchronous execution? (i.e., can they be safely called when the  
program was in the middle of a getmem, I/O operation, ...) Usually the  
most you should ever do inside a signal handler is set some global  
variable, because you have no idea what code is currently actually  
executing.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Joost van der Sluis
In reply to this post by Wimpie Nortje
On Mon, 2009-11-09 at 13:02 +0200, Wimpie Nortje wrote:

>
> Michael Van Canneyt wrote:
> > It should stop the daemons properly. This is the code that gets executed:
> >
> > Procedure DoShutDown(Sig : Longint; Info : PSigInfo; Context :
> > PSigContext);
> > cdecl;
> >
> > begin
> >   Application.StopDaemons(True);
> >   Application.Terminate;
> > end;
> >
> > If it doesn't, something is wrong :(
> I think something is wrong. :(
>
> For my TDaemon descendant I assigned the following events:
> OnDestroy
> OnExecute
> OnPause
> OnShutdown
> OnStart
> OnStop
>
> When I type "./daemon -r &"
> The following handlers are executed:
> 1. OnStart
> 2. OnExecute
>
> When I type "kill -TERM daemon_pid"
> The following handler is executed
> 1. OnDestroy
>
> I am also a bit confused by the wiki. According to the wiki Start and
> stop are triggered under linux but shutdown is not. The 'Taming the
> daemon' article does not specify which of these 3 are called under linux.
>
> As I mentioned earlier, I use FPC 2.2.4. Could that be the problem?

I don't know how I did it exactly, but I ahve this working properly in a
production system. (Even created my own SysV init script for it. It
should be generally usable for all fpc-daemon applications)

But I did use fpc 2.3.1, so try the new 2.4.0rc1. And I think that it
won't work always now I've read Jonas' mail about it. Maybe I'll look at
that later.

Joost.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Michael Van Canneyt


On Mon, 9 Nov 2009, Joost van der Sluis wrote:

> On Mon, 2009-11-09 at 13:02 +0200, Wimpie Nortje wrote:
>>
>> Michael Van Canneyt wrote:
>>> It should stop the daemons properly. This is the code that gets executed:
>>>
>>> Procedure DoShutDown(Sig : Longint; Info : PSigInfo; Context :
>>> PSigContext);
>>> cdecl;
>>>
>>> begin
>>>   Application.StopDaemons(True);
>>>   Application.Terminate;
>>> end;
>>>
>>> If it doesn't, something is wrong :(
>> I think something is wrong. :(
>>
>> For my TDaemon descendant I assigned the following events:
>> OnDestroy
>> OnExecute
>> OnPause
>> OnShutdown
>> OnStart
>> OnStop
>>
>> When I type "./daemon -r &"
>> The following handlers are executed:
>> 1. OnStart
>> 2. OnExecute
>>
>> When I type "kill -TERM daemon_pid"
>> The following handler is executed
>> 1. OnDestroy
>>
>> I am also a bit confused by the wiki. According to the wiki Start and
>> stop are triggered under linux but shutdown is not. The 'Taming the
>> daemon' article does not specify which of these 3 are called under linux.
>>
>> As I mentioned earlier, I use FPC 2.2.4. Could that be the problem?
>
> I don't know how I did it exactly, but I ahve this working properly in a
> production system. (Even created my own SysV init script for it. It
> should be generally usable for all fpc-daemon applications)

That's good news: I had it on my todo list to let the 'Install' mode generate
this script in the appropriate directory. If I can use your script as a
start, then I can implement that.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Wimpie Nortje
In reply to this post by Joost van der Sluis


Joost van der Sluis wrote:
On Mon, 2009-11-09 at 13:02 +0200, Wimpie Nortje wrote:
  
Michael Van Canneyt wrote:
    
It should stop the daemons properly. This is the code that gets executed:

Procedure DoShutDown(Sig : Longint; Info : PSigInfo; Context : 
PSigContext);
cdecl;

begin
  Application.StopDaemons(True);
  Application.Terminate;
end;

If it doesn't, something is wrong :(
      
I think something is wrong. :(

For my TDaemon descendant I assigned the following events:
OnDestroy
OnExecute
OnPause
OnShutdown
OnStart
OnStop

When I type "./daemon -r &"
The following handlers are executed:
1. OnStart
2. OnExecute

When I type "kill -TERM daemon_pid"
The following handler is executed
1. OnDestroy

I am also a bit confused by the wiki. According to the wiki Start and 
stop are triggered under linux but shutdown is not. The 'Taming the 
daemon' article does not specify which of these 3 are called under linux.

As I mentioned earlier, I use FPC 2.2.4. Could that be the problem?
    

I don't know how I did it exactly, but I ahve this working properly in a
production system. (Even created my own SysV init script for it. It
should be generally usable for all fpc-daemon applications)

But I did use fpc 2.3.1, so try the new 2.4.0rc1. And I think that it
won't work always now I've read Jonas' mail about it. Maybe I'll look at
that later.


  
Thanks, I will try the new compiler.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Joost van der Sluis
In reply to this post by Michael Van Canneyt
On Mon, 2009-11-09 at 14:14 +0100, Michael Van Canneyt wrote:

>
> On Mon, 9 Nov 2009, Joost van der Sluis wrote:
> >
> > I don't know how I did it exactly, but I ahve this working properly in a
> > production system. (Even created my own SysV init script for it. It
> > should be generally usable for all fpc-daemon applications)
>
> That's good news: I had it on my todo list to let the 'Install' mode generate
> this script in the appropriate directory. If I can use your script as a
> start, then I can implement that.

See below. Description and daemonname/filenames should be adapted,
offcourse.

#!/bin/bash
#
# startupmaildaemon     startup script for  the IA-MailDaemon
#
# chkconfig: - 2345 20 80
# description: MailDaemon wordt gebruikt om mailtjes die geagendeerd zijn om  \
#              te versturen in de IA ook daadwerkelijk te versturen
# processname: maildaemon
# pidfile: /var/run/maildaemon.pid

. /etc/rc.d/init.d/functions

prog=MailDaemon
maildaemon=${MAILDAEMON-/opt/maildaemon/maildaemon}
pidfile=${PIDFILE-/var/run/maildaemon.pid}
lockfile=${LOCKFILE-/var/lock/subsys/maildaemon}
runasuser=${RUNUSER-apache}
RETVAL=0

start() {
        echo -n $"Starting $prog: "
        sudo -b -u $runasuser $maildaemon -b -r
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}


stop() {
        echo -n $"Stopping $prog: "
        killproc -d 10 $maildaemon
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}


case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  *)
         echo "Usage: $0 {start|stop|restart}"
         exit 1
esac
exit 0


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Henry Vermaak
In reply to this post by Michael Van Canneyt
2009/11/9 Michael Van Canneyt <[hidden email]>:
>
> That's good news: I had it on my todo list to let the 'Install' mode
> generate
> this script in the appropriate directory. If I can use your script as a
> start, then I can implement that.

I've got /etc/init.d/skeleton (in debian), which looks like a very
good place to start.

Henry
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Vinzent Höfler
In reply to this post by Michael Van Canneyt
Michael Van Canneyt <[hidden email]>:

> On Mon, 9 Nov 2009, Wimpie Nortje wrote:
>
> It should stop the daemons properly. This is the code that gets executed:
>
> Procedure DoShutDown(Sig : Longint; Info : PSigInfo; Context :
> PSigContext);
> cdecl;
>
> begin
>    Application.StopDaemons(True);
>    Application.Terminate;
> end;
>
> If it doesn't, something is wrong :(

As wrong as in "Don't call most system functions from within a signal handler.", maybe?


Vinzent.

--
DSL-Preisknaller: DSL Komplettpakete von GMX schon für
16,99 Euro mtl.!* Hier klicken: http://portal.gmx.net/de/go/dsl02
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Michael Van Canneyt


On Mon, 9 Nov 2009, "Vinzent Höfler" wrote:

> Michael Van Canneyt <[hidden email]>:
>
>> On Mon, 9 Nov 2009, Wimpie Nortje wrote:
>>
>> It should stop the daemons properly. This is the code that gets executed:
>>
>> Procedure DoShutDown(Sig : Longint; Info : PSigInfo; Context :
>> PSigContext);
>> cdecl;
>>
>> begin
>>    Application.StopDaemons(True);
>>    Application.Terminate;
>> end;
>>
>> If it doesn't, something is wrong :(
>
> As wrong as in "Don't call most system functions from within a signal handler.", maybe?
Hmm... In that case, many exceptions would not work either ?

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Jonas Maebe-2

On 09 Nov 2009, at 21:03, Michael Van Canneyt wrote:

> On Mon, 9 Nov 2009, "Vinzent Höfler" wrote:
>
>> As wrong as in "Don't call most system functions from within a  
>> signal handler.", maybe?
>
> Hmm... In that case, many exceptions would not work either ?

The sigaction handlers for catching exceptions have been fixed quite a  
while ago already to only collect the necessary information from the  
signal context, and to then return from the signal handler to the  
start of the RTL function that handles run time errors, passing this  
info as arguments (we can pass at least 3 items on every platform by  
modifying the parameter register contents in the signal context; we  
also change the instruction pointer in the context so that it returns  
to the RTL's runerror handler rather than to wherever the signal was  
triggered).


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Stopping daemon in linux

Vinzent Höfler
Jonas Maebe <[hidden email]>:

> On 09 Nov 2009, at 21:03, Michael Van Canneyt wrote:
>
> > On Mon, 9 Nov 2009, "Vinzent Höfler" wrote:
> >
> >> As wrong as in "Don't call most system functions from within a  
> >> signal handler.", maybe?
> >
> > Hmm... In that case, many exceptions would not work either ?
>
> The sigaction handlers for catching exceptions have been fixed quite a  
> while ago already to only collect the necessary information from the  
> signal context, [...]

Additionally, signals causing run-time exceptions are triggered by the code itself, meaning things like SIGSEGV[*] are not actually "interrupting", rather they synchronously "transfer control" in a relatively well-defined way.

Maybe the analogy to good ol' DOS programming helps: Then signals are just interrupts, "synchronous" signals are "int xx" instructions, while "asynchronous" are real "hardware interrupts". Unfortunately you'll never know which will be which. ;)


Vinzent.

[*] Of course, the SIGSEGV still could have been sent from another process making it interrupting and asynchronous like any other signal.
--
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
12