Author: Eiko

Tags: open-rc, linux, init system, service, script, template

Time: 2024-10-03 13:18:41 - 2024-10-27 06:02:28 (UTC)

Depend

The depend() function declares the dependencies for a service script, for example your service must run after you have internet connections etc.

depend() {
    need net
    use dns logger netmount
    want coolservice
}

Here,

  • need for hard dependency, this forces net to start before this service starts.

  • use for soft dependency, only start the specified services if it is in the runlevel.

Default Functions

All services need to have start(), stop() and status() methods.

They have default implementations in lib/rc/sh/openrc-run.sh, allowing very compact service scripts owo, you can override them as needed. They will need you to provide the following variables

command=
command_args=
pidfile=

# optional:
# if your command runs foreground by default and needs a flag for turning into background
command_args_background=

# if your command does not know how to create a pid file, and you want the start-stop-daemon to create it for you owo
command_background=true

# if you want the start-stop-daemon to launch it as an unprivileged user
command_user="user:group"

Thus you do not need to write these three functions most of the time. For example

command="/usr/sbin/ntpd"

pidfile="/run/${RC_SVCNAME}.pid" # This RC_SVCNAME variable contains the name of the service
command_args="-p ${pidfile}"

Example: Using supervise-daemon To Automatically Restart Zerotier When It Crashes

By default openrc uses start-stop-daemon to manage services, which will not automatically restart services when it crashes. Instead the supervise-daemon can be used when you need the service to restart when crashes.

#!/sbin/openrc-run

command="/usr/bin/zerotier-one"
command_background=true
pidfile="/var/run/zerotier-one.pid"
name="ZeroTier One"
description="A network virtualization service"
supervisor="supervise-daemon"

depend() {
    need net
}

Reference

link