Sling Academy
Home/DevOps/Homebrew: How to Make a Service Auto-Start on System Boot

Homebrew: How to Make a Service Auto-Start on System Boot

Last updated: January 29, 2024

Introduction

Homebrew, also referred to as ‘brew’, is a powerful package manager for macOS and Linux systems that simplifies the process of installing, updating, and managing software. Despite its ease of use for handling software packages, managing system services and making them run on startup might not be immediately clear. Understanding how to control these services is fundamental for system administrators, developers, and even power users who want to ensure certain applications always start when their system boots.

In this tutorial, we’ll dive into how to use Homebrew to make a service auto-start on system boot, an essential task for maintaining background applications and services for your workflows on macOS and Linux.

Prerequisites

  • macOS or Linux operating system
  • Homebrew installed
  • Basic familiarity with using terminal or command line

Step-by-Step Instructions

Step 1: Installing a Service with Homebrew

Before making a service auto-start, you should first ensure that the service is actually installed. In Homebrew, this is often done with the brew install command.

$ brew install [service-name]

For example, to install the MariaDB database server, you would run:

$ brew install mariadb

Step 2: Understanding Homebrew Services

Homebrew provides a command brew services to manage services. This command reveals running services, allows starting and stopping services, and configures them to start on boot.

Step 3: Starting a Service with Homebrew

To start a service with Homebrew, run:

$ brew services start [service-name]

Starting the MariaDB service from our earlier example would look like this:

$ brew services start mariadb

Step 4: Setting a Service to Auto-Start on Boot

To configure a service to start automatically on boot is easy. The command you used to start the service does this by default. When executed, the service is set up to start on boot using the proper system daemon manager (like systemd or launchctl).

If you need to verify that a service is set to auto-start, you can inspect the list of Homebrew services:

$ brew services list

This command will reveal a table of services, their status, user, and whether they are started on boot (marked as ‘started’ under the Status column).

Step 5: Customizing Startup Options

Some services require or support passing custom options on startup. To include custom options when a service starts on boot, you’ll need to edit the .plist file associated with the service for macOS, or the .service file for Linux.

Advanced Usage: Creating Custom plists

For macOS, you can create a custom .plist file which can be saved at either ~/Library/LaunchAgents or /Library/LaunchDaemons.

This example demonstrates how to create a basic custom plist file to auto-start a fictional service with additional arguments:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.example.service</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/service</string>
    <string>--option-one</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

After creating this file, make sure to load it:

$ launchctl load ~/Library/LaunchAgents/com.example.service.plist

For Linux, you would typically manage auto-start services using systemd. Creating custom unit files in /etc/systemd/system/ can grant you more control over the service’s behavior on boot.

This simple systemd service file example auto-starts a network service:

[Unit]
Description=A simple example network service
After=network.target

[Service]
ExecStart=/usr/bin/python3 -m http.server
Restart=always

[Install]
WantedBy=multi-user.target

And enabling it on boot:

$ sudo systemctl enable example.service

Managing Services

Here are several commands used to manage services:

CommandDescription
brew services run [service]Runs the service but does not start it at boot.
brew services start [service]Starts and sets up the service to auto-start on boot.
brew services stop [service]Stops the service from running.
brew services restart [service]Stops and then starts the service.
brew services cleanupRemoves unused services from the list.

Managing services is effectively done through these Homebrew commands or by manually editing launch scripts.

Conclusion

In conclusion, Homebrew provides a simple and efficient way to make services auto-start on boot on macOS and Linux systems. With just a few commands or by editing configuration files, you can ensure that essential services are always running and available when you start your system.

Next Article: Homebrew Permission Denied Error: Could not symlink (4 solutions)

Previous Article: How to set a custom path for Homebrew

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide