Sign in Join

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

Mailing lists / lazarus / 2013-11

Linux service

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Antonio Fortuny

Hi Folks.

I have created a service which has to run into Windows and Linux servers. The program runs OK on both OS and the service runs nicely in Windows (32 and 64). I'm only a little bit stuck onto an UBUNTU platform. I've managed to launch and stop the service on an OpenSuse platform (thanks to Michael) using the standard tools startproc and killproc which do correctly teir job inside the service script when using the correct parameters. I'm trying to do the same in an UBUNTU script using this time start-stop-daemon. No problem to start the service but when stopping I still see the program running (simple ps -ef). Besides this, into the log produced by the service I read that the stop command has been caught and the program definitely reached the last line of the code. Buth the program still hangs somewhere as if the "unload" action didn't wok.
The run parameter "-r" has been given in the command which launches the program inside the script. Finally I need a kill -9 to get the program out. Maybe is this a normal behaviour but I guess not.
Another question: should I do something else in the code to have the service unloaded when stop is complete ?

Thanks,
Antonio.

Reply by email

Michael Van Canneyt
to Antonio Fortuny
Antonio Fortuny wrote 19 lines
Hi Folks. I have created a service which has to run into Windows and Linux servers. The program runs OK on both OS and the service runs nicely in Windows (32 and 64). I'm only a little bit stuck onto an UBUNTU platform. I've managed to launch and stop the service on an OpenSuse platform (thanks to Michael) using the standard tools startproc and killproc which do correctly teir job inside the service script when using the correct parameters. I'm trying to do the same in an UBUNTU script using this time start-stop-daemon. No problem to start the service but when stopping I still see the program running (simple ps -ef). Besides this, into the log produced by the service I read that the stop command has been caught and the program definitely reached the last line of the code. Buth the program still hangs somewhere as if the "unload" action didn't wok. The run parameter "-r" has been given in the command which launches the program inside the script. Finally I need a kill -9 to get the program out. Maybe is this a normal behaviour but I guess not. Another question: should I do something else in the code to have the service unloaded when stop is complete ?

Not from the pascal side.

Michael.

Reply by email

Patrick Chevalley

Hi,

To enable your program to react to signal you need to use fpSigAction. See here for usage: http://www.freepascal.org/docs-html/rtl/baseunix/fpsigaction.html

Instead of SigUsr1 in the example you can register a procedure for SIGTerm to handle a termination request. You can also add a procedure for SIGHUP if you want to implement a "service .. reload" action. The complete list of signal is in rtl/linux/signal.inc

By using SIGTerm (the default kill signal) you can cleanly close your program and releasing all resources, this is much better than using "kill -9" that stop the program abruptly.

Patrick

Reply by email

Michael Van Canneyt
to Patrick Chevalley
Patrick Chevalley wrote 8 lines
Hi, To enable your program to react to signal you need to use fpSigAction. See here for usage: http://www.freepascal.org/docs-html/rtl/baseunix/fpsigaction.html Instead of SigUsr1 in the example you can register a procedure for SIGTerm to handle a termination request. You can also add a procedure for SIGHUP if you want to implement a "service .. reload" action. The complete list of signal is in rtl/linux/signal.inc

That is exactly what the daemon code does.

Procedure DoShutDown(Sig : Longint; Info : PSigInfo; Context : PSigContext); cdecl;
begin
   Application.StopDaemons(True);
   Application.Terminate;
end;
Procedure SysInitDaemonApp;
Var
   old,new : SigactionRec;
begin
   ;
   fpSigaction(SIGQUIT, at New, at Old);
   fpSigaction(SIGTERM, at New, at Old);
   fpSigaction(SIGINT, at New, at Old);
end;

Michael.

Reply by email

Antonio Fortuny
to Michael Van Canneyt

Le 27/11/2013 15:06, Michael Van Canneyt a ?crit :

Show quoted text 37 lines
On Wed, 27 Nov 2013, Patrick Chevalley wrote: > Hi, > > To enable your program to react to signal you need to use fpSigAction. > See here for usage: > http://www.freepascal.org/docs-html/rtl/baseunix/fpsigaction.html > > Instead of SigUsr1 in the example you can register a procedure for > SIGTerm to handle a termination request. > You can also add a procedure for SIGHUP if you want to implement a > "service .. reload" action. > The complete list of signal is in rtl/linux/signal.inc That is exactly what the daemon code does. Procedure DoShutDown(Sig : Longint; Info : PSigInfo; Context : PSigContext); cdecl; begin Application.StopDaemons(True); Application.Terminate; end; Procedure SysInitDaemonApp; Var old,new : SigactionRec; begin ; fpSigaction(SIGQUIT, at New, at Old); fpSigaction(SIGTERM, at New, at Old); fpSigaction(SIGINT, at New, at Old); end;

Well, all this is already running. I managed to trap SIGTERM and SIGHUP to start the program shutdow an the config reload respectively and it works (# ./my_program -r & in background).
My only trouble was that the program looked like to be hung after it completely finished with its task (seen in the log file). Now, thanks to some misterious power (maybe the Force ?) doing: # bash -x my_service_script start
launches the service: new log started, all init tasks done, responds to requests and ... waits for jobs
# bash -x my_service_script stop
oh miracle, runs as well: the log file reflects all the tasks done on closing, the pidfile has gone and the process too ! (BTW, if you know some other good god who responds so fast that I could invoke, please let me know 8-) no Michael, not your turn yet)
I suspect that I wrote (or delete) something in the script that made it to work.
Last step, install a normal service into the UBUNTU box: this should be a piece of cake
Thanks anyway.

Antonio;
Show quoted text 9 lines
Michael. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email