- How to enable and start services on Alpine Linux
- Writing Init Scripts
- Introduction
- Things to avoid
- Minimal Templates
- Services relying on OpenRC exclusively
- Services supervised by s6
- Basic example
- start, stop, restart functions
- start
- OpenRC
- Available Runlevels
- rc-update usage
- rc-status usage
- rc-service usage
- rc usage
- Preventing slow services from delaying boot
- cgroups v2
- How to Restart a Service on Alpine Linux
- How to Restart a Service on Alpine Linux
- Conclusion
How to enable and start services on Alpine Linux
ow do I add or delete service at boot time on an Alpine Linux? How do I enable service such as Nginx/Apache at boot time on an Alpine Linux? How do I start/stop/restart services on an Alpine Linux?
Alpine Linux comes with OpenRC init system. This tutorial shows how to use the various command on OpenRC to manage services.
View status of all services
Type the following command:
Dynamic Runlevel: hotplugged
Dynamic Runlevel: needed/wanted
The default run level is called default, and it started crond and networking service for us.
Type the following command:
You can change run level using the rc command:
boot – Generally the only services you should add to the boot runlevel are those which deal with the mounting of filesystems, set the initial state of attached peripherals and logging. Hotplugged services are added to the boot runlevel by the system. All services in the boot and sysinit runlevels are automatically included in all other runlevels except for those listed here.
single – Stops all services except for those in the sysinit runlevel.
reboot – Changes to the shutdown runlevel and then reboots the host.
shutdown – Changes to the shutdown runlevel and then halts the host.
default – Used if no runlevel is specified. (This is generally the runlevel you want to add services to.)
To see manually started services, run:
To see crashed services, run:
How to list all available services
Type the following command:
# rc-service –list | grep -i nginx
If apache2/nginx not installed, try the apk command to install it:
How to add/enable service at boot time
To add apache2 service at boot time, run:
# rc-update add apache2 default
* service apache2 added to runlevel default
How to start/stop/restart services on Alpine Linux
The syntax is as as follows:
Writing Init Scripts
This material is work-in-progress .
Do not follow instructions here until this notice is removed.
(Last edited by Jirutka on 10 Apr 2022.)
Introduction
Alpine Linux uses the OpenRC init system to start services. Don’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 Gentoo’s repository for an existing initscript for your service. You can also check Gentoo’s wiki for some additional OpenRC information.
NOTE: OpenRC recently added documentation on how to write proper Init scripts. Make sure you read it!
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 openrc-run
Things to avoid
- Do not define supervisor=supervise-daemon in init.d script! This way the user cannot disable it without modifying the init.d script. If you really want to predefine supervise-daemon for particular service, define it in the corresponding conf.d file.
- Prefer standard OpenRC variables such as command_args , command_user etc. (see openrc-run(8)); do not create unnecessary config variables like FOO_OPTS , FOO_USER etc. If you want to predefine the default value in init.d script, use common idiom : $ .
- 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. cfgfile , cfgdir , logfile , cachedir , listen_on , start_wait , …).
Minimal Templates
Every init.d script you write needs to start with a shebang like:
Services relying on OpenRC exclusively
#!/sbin/openrc-run command=/path/to/command
Services supervised by s6
- Install and configure the s6-scan service to start on system boot
- Exclude start() , stop() and status() functions in order for s6 supervision to work reliably. OpenRC has built-in equivalent functions which invoke the necessary s6 commands.
- Include a depend() stanza to ensure that the s6-svscan service is already running.
- Add a start_pre() stanza to symlink the service directory into the scan directory, because the /etc/init.d/bootmisc scripts cleans out the /run directory on system boot.
#!/sbin/openrc-run name="foo" supervisor="s6" s6_service_path="$/s6-scan/$" depend() < need s6-svscan >start_pre() < if [ ! -L "$/s6-scan/$" ]; then ln -s "/path/to/$/service/dir" "$/s6-scan/$" fi >
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
OpenRC
The following commands are available to manage the init system:
poweroff # ⇔ shutdown now -P
Available Runlevels
The available runlevels are:
- default — Used if no runlevel is specified. (This is generally the runlevel you want to add services to.)
- hotplugged
- manual
The special runlevels are:
- sysinit — Brings up system specific stuff such as /dev , /proc and optionally /sys for Linux based systems. It also mounts /lib/rc/init.d as a ramdisk using tmpfs where available unless / is mounted rw at boot. rc uses /lib/rc/init.d to hold state information about the services it runs. sysinit always runs when the host first starts and should not be run again.
- boot — Generally the only services you should add to the boot runlevel are those which deal with the mounting of filesystems, set the initial state of attached peripherals and logging. Hotplugged services are added to the boot runlevel by the system. All services in the boot and sysinit runlevels are automatically included in all other runlevels except for those listed here.
- single — Stops all services except for those in the sysinit runlevel.
- reboot — Changes to the shutdown runlevel and then reboots the host.
- shutdown — Changes to the shutdown runlevel and then halts the host.
rc-update usage
Usage: rc-update [options] add service rc-update [options] del service rc-update [options] show Options: [suChqv] -s, --stack Stack a runlevel instead of a service -u, --update Force an update of the dependency tree -h, --help Display this help output -C, --nocolor Disable color output -v, --verbose Run verbosely -q, --quiet Run quietly
rc-status usage
Usage: rc-status [options] [runlevel1] [runlevel2] . Options: [aclrsuChqv] -a, --all Show services from all run levels -c, --crashed Show crashed services -l, --list Show list of run levels -r, --runlevel Show the name of the current runlevel -s, --servicelist Show service list -u, --unused Show services not assigned to any runlevel -h, --help Display this help output -C, --nocolor Disable color output -v, --verbose Run verbosely -q, --quiet Run quietly
rc-service usage
Usage: rc-service [options] Options: [e:ilr:ChqVv] -e, --exists tests if the service exists or not -i, --ifexists if the service exists then run the command -l, --list list all available services -r, --resolve resolve the service name to an init script -h, --help Display this help output -C, --nocolor Disable color output -V, --version Display software version -v, --verbose Run verbosely -q, --quiet Run quietly
rc usage
Usage: rc [options] Options: [a:o:s:SChqVv] -a, --applet runs the applet specified by the next argument -o, --override override the next runlevel to change into when leaving single user or boot runlevels -s, --service runs the service specified with the rest of the arguments -S, --sys output the RC system type, if any -h, --help Display this help output -C, --nocolor Disable color output -V, --version Display software version -v, --verbose Run verbosely -q, --quiet Run quietly
Preventing slow services from delaying boot
Services that take a while to start will block the boot process until they complete. E.g.: iwd and networking might delay startup of an interactive system rather than start in the background.
This can be remedied using async services. See stacked runlevels for more details on this.
cgroups v2
You can enable non-hybrid cgroups v2 by editing /etc/rc.conf and setting rc_cgroup_mode to unified.
to auto mount the cgroup filesystem on boot
How to Restart a Service on Alpine Linux
Operating methods incorporate a ton of providers to do your knowledge smoother. However, these providers in certain cases exhibit bugs and must be fastened. Hence, it’s good to restart the providers to put off the problems and let the providers work persistently. However, Linux distros like Ubuntu and Linux Mint supply a ordinary method to restart a service, in contrast to with Rocky Linux and Alpine Linux. This brief instruction briefly explains learn how to restart a service on Alpine Linux.
How to Restart a Service on Alpine Linux
We will see learn how to restart a working service on Alpine Linux employing a couple of ordinary instructions.
First, we listing each of the providers employing the subsequent rc command:
As you may see, each of the earlier providers are working in three forms of runlevels: default, essential, and handbook. The earlier command indicates each of the providers on the latest runlevel by default.
You can restart the providers which might be working in each of the said runlevels. The usual syntax to restart any service on Alpine Linux is as follows:
Moreover, you may run the subsequent command to restart the service:
/ and many others / init.d / < service-name >restart
First of all, we restart the “udev” service that runs beneath the default runlevel with the assistance of the subsequent command:
Similarly, you may additionally restart the providers of the essential runlevel. We restart the “sysfs” from each of the providers which might be working on the essential runlevel of our system. For this, we use the subsequent command:
/ and many others / init.d / sysfs restart
Lastly, we restart the “openrc-settingsd” service from the handbook runlevel by working the subsequent command:
rc-service openrc-settingsd restart
In this manner, you may restart the providers of Alpine Linux.
Conclusion
We defined learn how to restart a service on Alpine Linux. Alpine Linux organizes providers based on their run tiers. The system to commence the providers of all run tiers is identical. In Alpine Linux, you may restart the providers employing two instructions: “/etc/init.d/ restart” and “rc-service restart”.
Here, now we have seen learn how to restart an Alpine Linux service employing the given instructions. With this instruction’s assist, you may with ease restart the Alpine Linux service.
I am a Linux server pro admin and enthusiast, an upcoming web developer, junior php developer.
I am currently a content creator for Holhol24, I love to play with Linux and all other distributions of it.
I loves working with computers and strongly believes in sharing knowledge. Follow me For more insight into what is happening in Tech.