Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 18 Dec 2005 11:03:56 +0000
From:      Matthew Seaman <m.seaman@infracaninophile.co.uk>
To:        Gustavo Parrini <notebook@tdf.com.br>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: How to make init(8) run a daemon to me??
Message-ID:  <43A5421C.9040100@infracaninophile.co.uk>
In-Reply-To: <43A4E20F.2050207@tdf.com.br>
References:  <43A4E20F.2050207@tdf.com.br>

next in thread | previous in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enig1EFFCC152A1BB891F6F764CD
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Gustavo Parrini wrote:
> Hi All,
> 
> I'm having a pretty hard time trying to figure out how
> can I get init(8) to run a daemon and respawn it if it
> crashes. By the time I was a Linux user, it was done by
> adding a line in /etc/inittab and it was all...
> 
> I know that it must be done in /etc/ttys but I couldn't
> find any syntax example after searching the FreeBSD manpages,
> maillists, Handbook and Google.
> 
> "man init" gives me this:
> 
> ...
> The *init* utility can also be used to keep arbitrary daemons running,
> automatically restarting them if they die.  In this case, the first field
> in the ttys(5) 
> <http://www.freebsd.org/cgi/man.cgi?query=ttys&sektion=5&apropos=0&manpath=FreeBSD+6.0-RELEASE+and+Ports>; 
> file must not reference the path to a configured device
> node and will be passed to the daemon as the final argument on its com-
> mand line.  This is similar to the facility offered in the AT&T System V
> UNIX //etc/inittab/.
> ...
> 
> Sounded a little cryptic to me... sorry...
> 
> Can anyone help me? If so, An example line would be fine.

Lets take sendmail as an example here -- just for something concrete to
write about, and not because sendmail needs this sort of treatment.

Now, you need 4 fields to go into the /etc/ttys file to make this work:

   name -- which terminal device to use

   getty -- the program to attach to that terminal

   type -- the terminal type

   status -- on or off

(See the comments in the /etc/ttys file for more detail on this)

So as an example /etc/ttys line try:

   sendmail "/usr/sbin/sendmail -L sm-mta -bB -q30m" network on

The first field here is an arbitrary name -- anything will do so long
as it doesn't collide with any of the terminal device names from /dev.

The second field is the command and arguments to run, encased in "double
quotes".  Note the '-bB' flag -- that's important and tells sendmail to
stay in the foreground.  You definitely don't want '-bd' here.

The third field -- terminal type -- is really irrelevant here, so just
choose something vaguely appropriate.

The fourth field tells init "yes, please do run the sendmail process".

Your confusion stems from the fact that what you are doing is running
a daemon process -- i.e. a process /without/ a controlling terminal --
from the configuration file for setting up the processes that manage
terminals.  Yes, it's a hack.  The SysV-ish /etc/inittab mechanism has
been better thought out in this regard.

Note too that when we say 'daemon' we don't mean a process that forks
itself on startup, which is part of the standard mechanism for a process
to dissociate itself from any controlling terminal (see daemon(3)). All
programs in the base system intended to run as daemons will do that, which
makes them completely unsuitable for running through this mechanism --
you'll just end up with init(8) spinning madly as it continually tries to
restart the process.  You can only use the /etc/ttys trick with a program
that stays in the foreground.  Many system daemons have a debug mode which
prevents them from doing the initial fork to dissociate themselves from the
terminal.

You might wish to investigate the sysutils/daemontools port, which is a 
set of programs developed by Dan Bernstein for doing (inter alia) pretty
much exactly this job: http://cr.yp.to/daemontools.html

	Cheers,

	Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.                       7 Priory Courtyard
                                                      Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey         Ramsgate
                                                      Kent, CT11 9PW

--------------enig1EFFCC152A1BB891F6F764CD
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (FreeBSD)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDpUIi8Mjk52CukIwRAxPfAKCRNju3j497d8TRelnnk+mCMohd/QCeJTJG
Sw94IW/nfsmmJjJn17bkC8A=
=eaHu
-----END PGP SIGNATURE-----

--------------enig1EFFCC152A1BB891F6F764CD--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?43A5421C.9040100>