OpenRC Service Management
Managing services with OpenRC - Gentoo's init system for declarative service management without systemd
OpenRC Service Management
What It Is
OpenRC is Gentoo’s default init system - a lightweight, dependency-based init system that manages service startup, shutdown, and runlevels. It’s the alternative to systemd that many Gentoo users prefer.
Why OpenRC?
OpenRC offers:
- Simplicity - Shell-based service scripts, easy to understand and modify
- Speed - Parallel service startup with dependency tracking
- UNIX Philosophy - Does one thing well, uses standard tools
- Flexibility - Works with any kernel, no PID 1 lock-in
- Transparency - Clear, readable init scripts
Critical Warning
Never use systemd commands on an OpenRC system:
| Wrong | Right |
|---|---|
systemctl start docker | rc-service docker start |
systemctl enable sshd | rc-update add sshd default |
journalctl -xe | Check /var/log/ files |
systemctl status nginx | rc-service nginx status |
Core Commands
Service Control
# Start a service
rc-service docker start
# Stop a service
rc-service nginx stop
# Restart a service
rc-service sshd restart
# Check service status
rc-service postgresql status
Runlevel Management
# Show all running services
rc-status
# Show services in a specific runlevel
rc-status default
# Add service to default runlevel (enable at boot)
rc-update add sshd default
# Remove service from runlevel (disable at boot)
rc-update del docker default
# Show which runlevels a service is in
rc-update show sshd
Available Runlevels
| Runlevel | Purpose |
|---|---|
sysinit | System initialization (mounting filesystems) |
boot | Early boot services (networking, logging) |
default | Normal operation services |
shutdown | Shutdown procedures |
Service Script Location
OpenRC service scripts live in /etc/init.d/:
ls /etc/init.d/
# Shows: docker, nginx, sshd, postgresql, etc.
Each script supports standard actions:
start- Start the servicestop- Stop the servicerestart- Stop then startstatus- Show if runningzap- Reset crashed service state
Writing a Simple Service Script
#!/sbin/openrc-run
# /etc/init.d/myservice
name="My Service"
description="Example service for demonstration"
command="/usr/bin/myservice"
command_args="--daemon"
pidfile="/run/${RC_SVCNAME}.pid"
command_background=true
depend() {
need net
after logger
}
Key Variables
| Variable | Purpose |
|---|---|
name | Human-readable name |
description | Short description |
command | The executable to run |
command_args | Arguments to pass |
pidfile | PID file location |
command_background | Run in background |
Dependency Functions
depend() {
need net # Hard dependency - net must be running
use logger # Soft dependency - use if available
after docker # Order - start after docker
before nginx # Order - start before nginx
}
Viewing Logs
OpenRC doesn’t have a centralized journal like systemd. Logs are in traditional locations:
# System messages
/var/log/messages
# Specific service logs
/var/log/nginx/error.log
/var/log/docker.log
# Boot messages
dmesg | less
# Real-time log monitoring
tail -f /var/log/messages
Common Patterns
Starting All Services
# Boot into default runlevel
openrc default
Checking What Failed
# Show services that failed to start
rc-status | grep crashed
# Reset crashed service
rc-service myservice zap
Parallel Startup
OpenRC starts independent services in parallel automatically. Dependencies ensure correct ordering.
Practical Examples
Docker Setup
# Install Docker
emerge app-containers/docker
# Start Docker
rc-service docker start
# Enable at boot
rc-update add docker default
# Verify
rc-service docker status
SSH Server
# Start SSH daemon
rc-service sshd start
# Enable at boot
rc-update add sshd default
# Check status
rc-service sshd status
Configuration
Service-specific configuration typically lives in /etc/conf.d/:
# /etc/conf.d/nginx
NGINX_CONFIGFILE="/etc/nginx/nginx.conf"
These files are sourced by the init script and can override defaults.
Compared to systemd
| Feature | OpenRC | systemd |
|---|---|---|
| Init scripts | Shell scripts | Unit files |
| PID 1 | Not required | Required |
| Logging | Traditional syslog | journald |
| Complexity | Simple | Complex |
| Customization | Edit scripts | Override directories |
| Dependencies | depend() function | Unit file directives |
Related Concepts
- Runlevels - System operational modes
- Init Scripts - Service control scripts
- Portage - Gentoo’s package manager (installs services)