Openrc

From Funtoo
Jump to navigation Jump to search

Service script

Mini howto

Let's start to write our openrc service script(/etc/init.d/mydaemon). The first line is:

#!/sbin/openrc-run

If we use another interpreter, then we may have troubles. Next we add command variable with our binary full path and the file is as follows:

#!/sbin/openrc-run

command="/usr/sbin/mydaemon"

That's all. Now our binary specified in command variable can be started with help of start-stop-daemon.

Next add description variable to supply a user some info about the service:

description="super essential service"

And /etc/init.d/mydaemon describe now shows us:

 * super essential service
 * healthcheck: no description
 * unhealthy: no description
 * cgroup_cleanup: Kill all processes in the cgroup

Great. If our service create a pid file, then we should specify it full path with pidfile variable:

pidfile="/var/run/mydaemon.pid"

This helps start-stop-daemon to detect an already running instance of mydaemon. Next add additional arguments for our daemon with help of command_args variable:

command_args="-c /etc/mydaemon.conf"

If we have special arguments to place the our daemon into the background(and/or force pidfile creation), then we should place it in command_args_background variable.

Next we can tell start-stop-daemon to launch our daemon as an unprivileged user/group with:

command_user="user:group"

Next add some checks

Extra commands

To add additional commands we must tell openrc about it and define a function with such name:

extra_commands="cmd1"
description_cmd1="do some additional action"
cmd1() {
  ...
}

Variables reference

We set for openrc

  • command="PATH_TO_BIN" - a full path to binary we want to start/stop.
  • command_args="COMMAND LINE ARGUMENTS" - command line arguments for our daemon.
  • command_args_background="COMMAND LINE ARGUMENTS" - command line arguments for our daemon to background itself and/or to create a pid file.
  • command_background=true - tell start-stop-daemon to create a pid file and force a daemon into the background by itself.
  • command_user="USER:GROUP" - tell start-stop-daemon to launch our daemon as specified user and group(:GROUP can be ommited).
  • description="DESC TEXT" - a text which is outputed on /etc/init.d/SCRIPT describe command.
  • description_CMD="CMD DESC TEXT" - a text which is outputed on /etc/init.d/SCRIPT describe command as CMD extra command description.
  • extra_commands="cmd1 cmd2 ..." - add specified additional actions to a service script.
  • extra_started_commands="cmd1 cmd2 ..." - add specified additional actions to a service script, which are only valid while a daemon is running(started).
  • name="SERVICE_NAME" - a service name used in messages that openrc output. By default equal to RC_SVCNAME.
  • pidfile="PATH_TO_PID" - a full path to a pid file created by our daemon or start-stop-daemon(see command_background).
  • start_stop_daemon_args="EXTRA ARGS" - pass extra arguments to start-stop-daemon.

Openrc set for us

  • RC_CMD - a command/action name(e.g. start/stop/status/etc).
  • RC_SVCNAME - a service script name(e.g. mydaemon.local).