<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.alpinelinux.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Eznix86</id>
	<title>Alpine Linux - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.alpinelinux.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Eznix86"/>
	<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/wiki/Special:Contributions/Eznix86"/>
	<updated>2026-04-29T16:09:21Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.40.0</generator>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Writing_Init_Scripts&amp;diff=30900</id>
		<title>Writing Init Scripts</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Writing_Init_Scripts&amp;diff=30900"/>
		<updated>2025-09-12T22:00:09Z</updated>

		<summary type="html">&lt;p&gt;Eznix86: Add missing docs for start, stop, restart, and Daemon, Forking, Logging&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Alpine Linux uses the [https://github.com/OpenRC/openrc OpenRC] init system to start services. Don&#039;t confuse OpenRC init with our system init (the first process that is executed aka pid 1). Many of the current init.d script found in Alpine Linux are taken from Gentoo. If you want to save time you could search [https://packages.gentoo.org/categories Gentoo&#039;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&#039;s wiki] for some additional OpenRC information.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;strong&amp;gt;NOTE&amp;lt;/strong&amp;gt;: OpenRC recently added [https://github.com/OpenRC/openrc/blob/master/service-script-guide.md documentation] on how to write proper Init scripts. Make sure you read it!&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Primary information about the OpenRC format can be found in the [https://manpages.org/openrc-run/8 OpenRC man page openrc-run].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
apk add openrc-doc&lt;br /&gt;
man openrc-run&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Things to avoid ==&lt;br /&gt;
&lt;br /&gt;
* Prefer standard OpenRC variables such as &amp;lt;code&amp;gt;command_args&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;command_user&amp;lt;/code&amp;gt; etc. (see [https://www.mankier.com/8/openrc-run openrc-run(8)]); do not create unnecessary config variables like &amp;lt;code&amp;gt;FOO_OPTS&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;FOO_USER&amp;lt;/code&amp;gt; etc. If you want to predefine the default value in init.d script, use common idiom &amp;lt;code&amp;gt;: ${command_user=foo}&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Use snake_case for naming extra configuration variables (to be consistent with the OpenRC variables and other init scripts in Alpine). Do not prefix them with the service name, try to be consistent with existing init scripts (e.g. &amp;lt;code&amp;gt;cfgfile&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cfgdir&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;logfile&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cachedir&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;listen_on&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;start_wait&amp;lt;/code&amp;gt;, …).&lt;br /&gt;
&lt;br /&gt;
== Minimal Templates ==&lt;br /&gt;
&lt;br /&gt;
Every init.d script you write needs to start with a [https://en.wikipedia.org/wiki/Shebang_(Unix) shebang] like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;#!/sbin/openrc-run&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Services relying on OpenRC exclusively ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/sbin/openrc-run&lt;br /&gt;
&lt;br /&gt;
command=/path/to/command&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Services supervised by [https://www.skarnet.org/software/s6/ s6] ===&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
&lt;br /&gt;
* Install and configure the &amp;lt;code&amp;gt;s6-scan&amp;lt;/code&amp;gt; service to start on system boot&lt;br /&gt;
* Exclude &amp;lt;code&amp;gt;start()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;stop()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;status()&amp;lt;/code&amp;gt; functions in order for s6 supervision to work reliably. OpenRC has built-in equivalent functions which invoke the necessary s6 commands.&lt;br /&gt;
* Include a &amp;lt;code&amp;gt;depend()&amp;lt;/code&amp;gt; stanza to ensure that the &amp;lt;code&amp;gt;s6-svscan&amp;lt;/code&amp;gt; service is already running.&lt;br /&gt;
* Add a &amp;lt;code&amp;gt;start_pre()&amp;lt;/code&amp;gt; stanza to symlink the service directory into the scan directory, because the &amp;lt;code&amp;gt;/etc/init.d/bootmisc&amp;lt;/code&amp;gt; scripts cleans out the &amp;lt;code&amp;gt;/run&amp;lt;/code&amp;gt; directory on system boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/sbin/openrc-run&lt;br /&gt;
&lt;br /&gt;
name=&amp;quot;foo&amp;quot;&lt;br /&gt;
supervisor=&amp;quot;s6&amp;quot;&lt;br /&gt;
s6_service_path=&amp;quot;${RC_SVCDIR}/s6-scan/${name}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
depend() {&lt;br /&gt;
    need s6-svscan&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
start_pre() {&lt;br /&gt;
    if [ ! -L &amp;quot;${RC_SVC_DIR}/s6-scan/${name}&amp;quot; ]; then&lt;br /&gt;
        ln -s &amp;quot;/path/to/${name}/service/dir&amp;quot; &amp;quot;${RC_SVCDIR}/s6-scan/${name}&amp;quot;&lt;br /&gt;
    fi&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The rest of the below basic example could be omitted, but that would most probably leave you with an non working initd script.&lt;br /&gt;
&lt;br /&gt;
== Basic example ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/sbin/openrc-run&lt;br /&gt;
  &lt;br /&gt;
name=$RC_SVCNAME&lt;br /&gt;
cfgfile=&amp;quot;/etc/$RC_SVCNAME/$RC_SVCNAME.conf&amp;quot;&lt;br /&gt;
command=&amp;quot;/usr/bin/my_daemon&amp;quot;&lt;br /&gt;
command_args=&amp;quot;--my-daemon-args&amp;quot;&lt;br /&gt;
command_user=&amp;quot;my_system_user&amp;quot;&lt;br /&gt;
pidfile=&amp;quot;/run/$RC_SVCNAME/$RC_SVCNAME.pid&amp;quot;&lt;br /&gt;
start_stop_daemon_args=&amp;quot;--args-for-start-stop-daemon&amp;quot;&lt;br /&gt;
command_background=&amp;quot;yes&amp;quot;&lt;br /&gt;
&lt;br /&gt;
depend() {&lt;br /&gt;
        need net&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
start_pre() {&lt;br /&gt;
        checkpath --directory --owner $command_user:$command_user --mode 0775 \&lt;br /&gt;
                /run/$RC_SVCNAME /var/log/$RC_SVCNAME&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== start, stop, restart functions ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
This is generally only necessary if you want to do something special which is not provided by the default start/stop/restart implementations.&lt;br /&gt;
&lt;br /&gt;
=== start ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
start() {&lt;br /&gt;
    ebegin &amp;quot;Starting mydaemon&amp;quot;&lt;br /&gt;
    start-stop-daemon --start \&lt;br /&gt;
        --exec /usr/sbin/mydaemon \&lt;br /&gt;
        --pidfile /var/run/mydaemon.pid \&lt;br /&gt;
        -- \&lt;br /&gt;
        --args-for-mydaemon&lt;br /&gt;
    eend $?&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== stop ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
stop() {&lt;br /&gt;
    ebegin &amp;quot;Stopping mydaemon&amp;quot;&lt;br /&gt;
    start-stop-daemon --stop \&lt;br /&gt;
        --exec /usr/sbin/mydaemon \&lt;br /&gt;
        --pidfile /var/run/mydaemon.pid&lt;br /&gt;
    eend $?&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== restart ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
restart() {&lt;br /&gt;
    ebegin &amp;quot;Restarting mydaemon&amp;quot;&lt;br /&gt;
    svc_stop&lt;br /&gt;
    svc_start&lt;br /&gt;
    eend $?&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Daemon, Forking, Logging ==&lt;br /&gt;
&lt;br /&gt;
If your daemon supports running in the foreground, set &amp;lt;code&amp;gt;command_background=&amp;quot;yes&amp;quot;&amp;lt;/code&amp;gt; and add a &amp;lt;code&amp;gt;--no-fork&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--foreground&amp;lt;/code&amp;gt; option to &amp;lt;code&amp;gt;command_args&amp;lt;/code&amp;gt;. This prevents the daemon from forking itself into the background, so OpenRC can handle backgrounding.&lt;br /&gt;
&lt;br /&gt;
When you use &amp;lt;code&amp;gt;command_background=&amp;quot;yes&amp;quot;&amp;lt;/code&amp;gt;, you must also set a &amp;lt;code&amp;gt;pidfile&amp;lt;/code&amp;gt;. This allows OpenRC to track and manage the daemon process correctly.&lt;br /&gt;
&lt;br /&gt;
If your daemon does not support running in the foreground, you can use &amp;lt;code&amp;gt;start-stop-daemon&amp;lt;/code&amp;gt; directly in your &amp;lt;code&amp;gt;start()&amp;lt;/code&amp;gt; function to launch the service and manage forking and pidfiles manually.&lt;br /&gt;
&lt;br /&gt;
For logging, use a &amp;lt;code&amp;gt;logfile&amp;lt;/code&amp;gt; variable if your daemon supports it, or redirect output in your &amp;lt;code&amp;gt;start()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
Example for a daemon that supports foreground mode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
command_args=&amp;quot;--no-fork --logfile /var/log/my_daemon.log&amp;quot;&lt;br /&gt;
command_background=&amp;quot;yes&amp;quot;&lt;br /&gt;
pidfile=&amp;quot;/run/my_daemon.pid&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example for a daemon that does not support foreground mode, using &amp;lt;code&amp;gt;start-stop-daemon&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
start() {&lt;br /&gt;
    ebegin &amp;quot;Starting mydaemon&amp;quot;&lt;br /&gt;
    start-stop-daemon --start \&lt;br /&gt;
        --exec /usr/bin/mydaemon \&lt;br /&gt;
        --pidfile /run/mydaemon.pid \&lt;br /&gt;
        --background \&lt;br /&gt;
        --make-pidfile \&lt;br /&gt;
        -- \&lt;br /&gt;
        --logfile /var/log/mydaemon.log&lt;br /&gt;
    eend $?&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Booting]]&lt;/div&gt;</summary>
		<author><name>Eznix86</name></author>
	</entry>
</feed>