Writing Init Scripts: Difference between revisions

From Alpine Linux
Line 13: Line 13:
<code>man openrc-run</code>
<code>man openrc-run</code>


== Mandatory ==
== Minimal Templates ==


Every init.d script you write needs to start with a [https://en.wikipedia.org/wiki/Shebang_(Unix) shebang] like:
Every init.d script you write needs to start with a [https://en.wikipedia.org/wiki/Shebang_(Unix) shebang] like:
Line 19: Line 19:
<code>#!/sbin/openrc-run</code>
<code>#!/sbin/openrc-run</code>


=== Minimal template for services relying on OpenRC exclusively ===
=== Services relying on OpenRC exclusively ===


<pre>
<pre>
Line 27: Line 27:
</pre>
</pre>


=== Minimal template for services supervised by [http://www.skarnet.org/software/s6/ s6] ===
=== Services supervised by [http://www.skarnet.org/software/s6/ s6] ===


Note that you must '''exclude start, stop and status functions''' in order for s6 supervision to work, as OpenRC has replacement functions for these which interact reliably with the various s6 commands, without having to call them explicitly.
Note that you must '''exclude start, stop and status functions''' in order for s6 supervision to work, as OpenRC has replacement functions for these which interact reliably with the various s6 commands, without having to call them explicitly.

Revision as of 20:22, 10 December 2017

This material is work-in-progress ...

Do not follow instructions here until this notice is removed.
(Last edited by Zoot on 10 Dec 2017.)

Introduction

Alpine Linux uses the OpenRC init system to start services. Don't confuse OpenRC init with out system init (the first process that is executed aka pid 1). Many of the current init.d script found in Alpine Linux are takes from Gentoo. If you want to save time you could search Gentoo's repository for an existing initscript for your service. You can also check Gentoo's wiki for some additional OpenRC information.

If you cannot find an init.d script from Gentoo, or you just want to start to write your own init.d scripts, we provide you with some basic information on how to write simple OpenRC init scripts.

Primary information about the OpenRC format can be found in the OpenRC man page openrc-run.

apk add openrc-doc man

man openrc-run

Minimal Templates

Every init.d script you write needs to start with a shebang like:

#!/sbin/openrc-run

Services relying on OpenRC exclusively

#!/sbin/openrc-run

command="/path/to/command"

Services supervised by s6

Note that you must exclude start, stop and status functions in order for s6 supervision to work, as OpenRC has replacement functions for these which interact reliably with the various s6 commands, without having to call them explicitly.

#!/sbin/openrc-run

name="foo"
supervisor="s6"
s6_service_path="${RC_SVCDIR}/s6-scan/${name}"

depend() {
    need s6-svscan
}

The rest of the below basic example could be omitted, but that would most probably leave you with an non working initd script.

Basic example

#!/sbin/openrc-run
  
name=$RC_SVCNAME
cfgfile="/etc/$RC_SVCNAME/$RC_SVCNAME.conf"
command="/usr/bin/my_daemon"
command_args="--my-daemon-args"
command_user="my_system_user"
pidfile="/run/$RC_SVCNAME/$RC_SVCNAME.pid"
start_stop_daemon_args="--args-for-start-stop-daemon"
command_background="yes"

depend() {
        need net
}

start_pre() {
        checkpath --directory --owner $command_user:$command_user --mode 0775 \
                /run/$RC_SVCNAME /var/log/$RC_SVCNAME
}

start, stop, restart functions

OpenRC defined a few basic functions ie: start, stop, restart. These functions are defined by default but can be overwritten by defining your own set of functions. This is generally only necessary if you want to do something special which is not provided by the default start/stop/restart implementations.

start

start() {
    ebegin "Starting mydaemon"
    start-stop-daemon --start \
        --exec /usr/sbin/mydaemon \
        --pidfile /var/run/mydaemon.pid \
        -- \
        --args-for-mydaemon
    eend $?
}

stop

restart

Daemon, Forking, Logging

TODO...