Writing Init Scripts: Difference between revisions

From Alpine Linux
(Change runscript to openrc-run, add start_stop_daemon_args to example)
No edit summary
Line 1: Line 1:
{{Draft}}
{{Draft}}


This document explains how to write openrc scripts, aka init.d scripts.
== Introduction ==


Needs to start with <tt>#!/sbin/openrc-run</tt>
Alpine Linux uses the [https://github.com/OpenRC/openrc 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 [https://packages.gentoo.org/categories Gentoo's repository] for an existing initscript for your service. You can also check [https://wiki.gentoo.org/wiki/Handbook:X86/Working/Initscripts#Writing_initscripts Gentoo's wiki] for some additional OpenRC information.


Using the start/stop templates and special predefined variables:
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 [http://manpages.org/openrc-run/8 OpenRC man page openrc-run].
 
<code>apk add openrc-doc man</code>
 
<code>man openrc-run</code>
 
== Basics ==
 
Every init.d script you write needs to start with a [https://en.wikipedia.org/wiki/Shebang_(Unix) shebang] like:
 
<code>#!/sbin/openrc-run</code>
 
== Basic example ==


<pre>
<pre>
description="My Example Daemon"
#!/sbin/openrc-run
command="/usr/sbin/mydaemon"
 
command_args="--args-for-mydaemon"
name=$RC_SVCNAME
command="/usr/bin/my_daemon"
command_args="--my-daemon-args"
pidfile="/run/$RC_SVCNAME/$RC_SVCNAME.pid"
command_user="my_system_user"
start_stop_daemon_args="--args-for-start-stop-daemon"
start_stop_daemon_args="--args-for-start-stop-daemon"
pidfile="/var/run/mydaemon.pid"
command_background="yes"
stopsig="SIGTERM"
 
depend() {
        need net
}
 
start_pre() {
        checkpath --directory --owner $command_user:$command_user --mode 0775 \
                /run/$RC_SVCNAME /var/log/$RC_SVCNAME
}
</pre>
</pre>


Line 30: Line 56:
}
}
</pre>
</pre>
More info here:
http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=2&chap=4#doc_chap4


[[Category:Booting]]
[[Category:Booting]]

Revision as of 09:05, 20 May 2017

This material is work-in-progress ...

Do not follow instructions here until this notice is removed.
(Last edited by Clandmeter on 20 May 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

Basics

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

#!/sbin/openrc-run

Basic example

#!/sbin/openrc-run
  
name=$RC_SVCNAME
command="/usr/bin/my_daemon"
command_args="--my-daemon-args"
pidfile="/run/$RC_SVCNAME/$RC_SVCNAME.pid"
command_user="my_system_user"
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
}

Using start-stop-daemon

To be written

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