As mentioned in other answers, Dan Bernstein's daemontools began a whole family of toolsets that share the same raw mechanisms:
Under pretty much any of them, one writes a run
program that runs/is the dæmon, and a service manager or supervisor process simply monitors it as a forked child process using the normal Unix and Linux mechanism. For Apache and MySQL, you are treading where a lot of people have trodden before, and there are a lot of examples of how to run these servers under daemontools-family service management. Here are only some:
Chris Down suggests that the larger toolsets may be unsuitable. This isn't really the case. Whilst all of these toolsets are coherent and self-consistent, none of them demand that one use any of the tools other than the ones needed in any particular situation. One can also mix and match. One can use Laurent Bercot's execlineb
and all of its utilities under perp, or my nosh
script interpreter and all of its utilities under runit; just as one can equally use Gerrit Pape's chpst
under my service-manager
.
Equally, you could run Apache and MySQL from systemd (if you are Linux-only) or launchd (if you are using MacOS 10). A launchd configuration file is rather complex and cumbersome, by comparison to the other systems mentioned. systemd unit files, however, are on the same order of simplicity as run
scripts:
There are a fair few home-brewed mysqld.service
and httpd.service
service units to be found on the World Wide Web, in various people's collections of home-brewed handy systemd service units.
All of these provide the basic substrate of starting a dæmon at bootstrap, stopping and starting it under administrator/automated control whilst the system is running, and automatically restarting it in various failure cases. Xion345 makes the mistake of conflating this with monit. As you can see in xyr answer, the substrate there for monitoring and controlling is dæmon System 5 rc
. It could equally well have been systemd or nosh. (In fact, if Xion345's example had used the service
command instead of trying to run init.d scripts directly, which isn't a good idea for this and several other reasons, it would have worked pretty much as-is with systemd, upstart, or nosh.)
Where monit belongs is in the layer above. monit uses the start/stop/supervision substrate, and monitors the actual service provided, which one does in addition to supervising the dæmon process with a dæmon supervisor. In this layer one finds related tools such as nagios. (One can fairly easily plumb nagios into a daemontools-family-monitored dæmon, and have it check process state and uptimes via the daemontools API. My nosh toolset even comes with one basic nagios plug-in for doing this.)