Starting A Daemon via daemon(8) in FreeBSD
Starting A Daemon via daemon(8) in FreeBSD
Daemonizing an application that must persist across reboots while including native process management capability is easily accomplished by combining the functions of rc(8) scripts and commands with daemon(8), a utility that detaches itself from the controlling terminal and executing a program. For example, a service written in Python implementing an HTTP API front-end.
Combing these two utilities enables automated process initialization during system boot and provides the ability to stop, start, and restart services manually using either the rc(8) script or service(8). For example:
/usr/local/etc/rc.d/httpapi start
or
service httpapi start
Create an rc(8) script in /usr/local/etc/rc.d
with a name descriptive of the application. httpapi
is a descriptive name in this case. Writing the rc(8) script is covered extensively well in the “Practical rc.d scripting in BSD” article on FreeBSD.org and is a very good read. Those reading this blog are encouraged to go read that article though, for expediency, a basic implementation may look very similar to:
#!/bin/sh# REQUIRE: LOGIN
. /etc/rc.subr
name=httpapi
rcvar=httpapi_enable
command=/usr/local/bin/httpapi
command_interpreter=python
httpapi_user=httpapi
start_cmd=”/usr/sbin/daemon -u $httpapi_user $command”load_rc_config $name
run_rc_command “$1”
The value of the key rcvar
is used to automate the startup of the daemon at system boot within /etc/rc.conf
. Append a line to the file containing a combination of this value as key a key with a boolean “YES” or “NO” similarly to:
echo "httpapi_enable=\"YES\"" >> /etc/rc.conf
Disclaimer
Data and information described on this blog are for informational purposes only. The author of this blog provides no warranty/guarantee, expressed or implied, that this data and information will function as described here. Readers are expected to exercise due diligence when researching, developing, and deploying techniques and methods for use within their environments.
Comments posted are the explicit opinions of the comment poster themselves and does not necessarily reflect the views and opinions of the author of this blog.
-
January 31, 2015 at 2:26 PMIn Other BSDs for 2015/01/31 – DragonFly BSD Digest