Running Services with Systemd

What are systemd Service Files?

systemd service files are plain text files that define how a service should be started, stopped, and managed. These files typically have a .service extension and are stored in specific directories, such as /etc/systemd/system/ or /usr/lib/systemd/system/.

Basic Structure of a Service File

Let’s start with a simple example of a systemd service file:

[Unit]
Description=My Custom Service
After=network.target

[Service]
ExecStart=/usr/bin/my-custom-service
Restart=on-failure

[Install]
WantedBy=multi-user.target

This basic structure consists of three main sections: [Unit], [Service], and [Install]. Let’s break down each section and explore some common options.

The [Unit] Section

The [Unit] section contains metadata about the service and defines its relationships with other units.

Common options include:

  • Description: A human-readable description of the service.
  • After: Ensures the service starts after the specified units.
  • Requires: Lists units that must be started along with this service.
  • Wants: Similar to Requires, but less strict (failure doesn’t stop this service).

Example with more options:

[Unit]
Description=Advanced Custom Service
After=network.target mysql.service
Requires=mysql.service
Wants=redis.service

The [Service] Section

The [Service] section is where you define how the service should be run and managed.

Key options include:

  • ExecStart: The command to start the service.
  • ExecStop: The command to stop the service (if not specified, systemd sends SIGTERM).
  • Restart: Defines when the service should be restarted.
  • User: The user under which the service should run.
  • WorkingDirectory: The working directory for the service.

Let’s see an example with more options:

[Service]
ExecStart=/usr/bin/my-advanced-service --config /etc/myservice/config.yml
ExecStop=/usr/bin/my-advanced-service --shutdown
Restart=always
RestartSec=5
User=myservice
Group=myservice
WorkingDirectory=/var/lib/myservice
Environment="DEBUG=1" "LOG_LEVEL=info"

The [Install] Section

The [Install] section is used when enabling the service to start automatically at boot.

Common options:

  • WantedBy: Specifies the target unit that should include this service.
  • RequiredBy: Similar to WantedBy, but creates a stronger dependency.

Example:

[Install]
WantedBy=multi-user.target
RequiredBy=another-custom.service

Advanced Configuration Options

systemd offers many more options for fine-tuning service behavior. Here are a few advanced examples:

Resource Management

You can limit the resources available to a service:

[Service]
CPUQuota=50%
MemoryLimit=1G
TasksMax=100

Dependency Management

Control how your service interacts with others:

[Unit]
BindsTo=container.service
PartOf=application-group.target

[Service]
ExecStartPre=/usr/bin/docker pull myapp:latest
ExecStart=/usr/bin/docker run myapp:latest
ExecStop=/usr/bin/docker stop myapp

Security Features

Enhance the security of your service:

[Service]
PrivateTmp=true
ProtectSystem=full
ReadOnlyDirectories=/var
ReadWriteDirectories=/var/lib/myapp
CapabilityBoundingSet=CAP_NET_BIND_SERVICE

Conclusion

systemd service files provide a powerful and flexible way to manage services on Linux systems. By understanding the various options available, you can create robust and well-behaved services that integrate seamlessly with the systemd ecosystem.

Remember to use systemctl commands to manage your services:

  • systemctl start myservice.service
  • systemctl stop myservice.service
  • systemctl enable myservice.service
  • systemctl disable myservice.service

Conclusion

All software needs to be run in order to do something useful, and long-running services are not uncommon. If you want to run a long running service, you’ll most likely be using a Linux server. And if you use a Linux server, you’ll most likely be using a distro that uses Systemd. Knowing how you can write service files and deploy services with Systemd makes it really easy to deploy software and all you need to do is write one file for it all to magically work!

関連記事

カテゴリー:

ブログ

情シス求人

  1. チームメンバーで作字やってみた#1

ページ上部へ戻る