Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-11

Stopping daemon in linux

23 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

Marco van de Voort
to Wimpie Nortje

In our previous episode, Wimpie Nortje said:

Show quoted text 7 lines
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.

Reply by email

Wimpie Nortje
to Marco van de Voort
Marco van de Voort wrote 16 lines
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

Reply by email

Marc Santhoff
to Wimpie Nortje: Hi I am writing an application using lazdaemon. How does one stop the daemon gracefully,…
Wimpie Nortje wrote 8 lines
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

Reply by email

Marco van de Voort
to Wimpie Nortje: Are there plans to fix/change this, or must I implement some kind of pid check myself? I…

In our previous episode, Wimpie Nortje said:

Show quoted text 19 lines
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.

Reply by email

Wimpie Nortje
to Marco van de Voort
Marco van de Voort wrote 5 lines
> 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.

Reply by email

Wimpie Nortje
to Marc Santhoff: You can send any defined signal using kill. Try: # kill -TERM <yourdeamon> for sending…
Marc Santhoff wrote 7 lines
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.

Reply by email

Michael Van Canneyt
to Wimpie Nortje
Wimpie Nortje wrote 13 lines
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.

Reply by email

Marco van de Voort
to Wimpie Nortje: I know there is a standard/recomended way to write a daemon. If you use KDevelop it will…

In our previous episode, Wimpie Nortje said:

Show quoted text 5 lines
> 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.

Reply by email

Wimpie Nortje
to Michael Van Canneyt: It should stop the daemons properly. This is the code that gets executed: Procedure…
Michael Van Canneyt wrote 12 lines
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?

Reply by email

Jonas Maebe
to Michael Van Canneyt: It should stop the daemons properly. This is the code that gets executed: Procedure…
Michael Van Canneyt wrote 13 lines
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

Reply by email

Joost van der Sluis
to Wimpie Nortje: I think something is wrong. :( For my TDaemon descendant I assigned the following events:…
Wimpie Nortje wrote 38 lines
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.

Reply by email

Michael Van Canneyt
to Joost van der Sluis
Joost van der Sluis wrote 43 lines
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.

Reply by email

Wimpie Nortje
to Joost van der Sluis: I don't know how I did it exactly, but I ahve this working properly in a production…
Joost van der Sluis wrote 53 lines
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.

Reply by email

Joost van der Sluis
to Michael Van Canneyt: That's good news: I had it on my todo list to let the 'Install' mode generate this script…
Michael Van Canneyt wrote 10 lines
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

Reply by email

Vinzent Höfler
to Michael Van Canneyt: It should stop the daemons properly. This is the code that gets executed: Procedure…

Michael Van Canneyt :

Show quoted text 14 lines
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

Reply by email

Michael Van Canneyt
to Vinzent Höfler
"Vinzent Höfler" wrote 18 lines
Michael Van Canneyt : > 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.

Reply by email

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote 6 lines
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

Reply by email

Vinzent Höfler
to Jonas Maebe

Jonas Maebe :

Show quoted text 12 lines
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

Reply by email

Michael Van Canneyt
to Jonas Maebe: The sigaction handlers for catching exceptions have been fixed quite a while ago already…
Jonas Maebe wrote 18 lines
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).

Hm.
Quite complex code, and not processor independent. Is there not a more generic way of handling this ?

Michael.

Reply by email

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote
Hm. Quite complex code, and not processor independent.

It never was processor-independent, because the siginfo codes already varied between architectures.

Is there not a more generic way of handling this ?

I doubt it.

Jonas

Reply by email

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 7 lines
On 09 Nov 2009, at 21:51, Michael Van Canneyt wrote: > Hm. Quite complex code, and not processor independent. It never was processor-independent, because the siginfo codes already varied between architectures.

I remember days when it was processor-independent, because the initial signal handlers only had the signal numbers as an argument.

But that's many many winters ago :-)

> Is there not a more generic way of handling this ? I doubt it.

Very bad :(

Michael.

Reply by email