[fpc-pascal] respawn a daemon in Linux

Tony Whyman tony.whyman at mccallumwhyman.com
Tue Jan 14 11:35:14 CET 2014


Michael,

This should be a classic Unix program with two separate parent and child
processes, where the child process runs your program and the parent is
responsible for monitoring the child and respawning it when it stops.
The basic algorithm of the parent is:

Begin
  ...
  daemon(0,0):
..
  repeat
    fork();
   if child then exec child program;
   else
     wait for child exit;
  log child completion;
 until sig term received;
...
End.

You need to set up signal handling to check for the sig term. Most
examples will be in 'C' but you should be able to write the same
algorithm in Pascal.

Good luck.

Tony Whyman
MWA



On 14/01/14 09:49, Michael Schnell wrote:
> Hi Linux Experts.
>
> Since years I am running an fpc program as a kind of daemon on a PC
> Linux server.
>
> I simply defined a "respawn" line in /etc/inittab and with any start
> of the server, the program happily automatically runs as a daemon.
>
> Now I migrated the the PC Linux server to an ARM based "QNAP" NAS box,
> which comes with a propriety - but rather normal - Linux distribution.
> Hence I compiled the program with the appropriate ARM fpc and it
> happily runs on this box, as well.
>
> Now the problem is that I can't edit the inittab file on this box, as
> it is in a RAM file system that gets recreated with any start.
>
> But I was able to install an init.d system that allows for running any
> script after booting. Thus I need to find a way to run my program as a
> deamon.
>
> Of course I do know about daemonapp.pp and with that, I suppose I
> could easily have the program daemonize itself. But the program is
> written in a way that it exits whenever some kind of error happens
> (and this does occur, as it accesses multiple sites via TCP/IP. So it
> relies on the "respawn" feature, inittab provides.
>
> I would appreciate any idea on how to create some kind of "respawn"
> algorithm within this program or with a second program - maybe just a
> script. In fact I could use "cron" for this, as happily the system
> does allow me to edit crontab (which here actually is
> /etc/config/crontab and the /etc/config/ is symlinked to a directory
> residing in a special location on a hard disk.
>
> -Michael
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal




More information about the fpc-pascal mailing list