If you have a binary executable file that you want to run on Linux as a service, you might be wondering how to do it. A service is a program that runs in the background and can be started, stopped, restarted, or queried by using commands like systemctl
or service
. A service can also be configured to start automatically when the system boots up.
There are different ways to create and manage services on Linux, but one of the most common and modern ones is using systemd. Systemd is an init system that manages the startup and shutdown of services and other processes on Linux. It also provides features like logging, dependency management, resource control, and more.
In this blog post, we will show you how to deploy a binary on Linux as a service using systemd. We will use Ubuntu 18.04 as an example, but the steps should be similar for other Linux distributions that use systemd.
Step 1: Create a systemd service file
The first step is to create a systemd service file that defines how your binary should run as a service. A systemd service file is a plain text file that has a .service
extension and follows a specific format. You can place your service file in /etc/systemd/system/
directory, which is where custom services are usually stored.
A basic systemd service file looks something like this:
[Unit]
Description=My Binary Service
After=network.target
[Service]
Type=simple
ExecStart=/path/to/my/binary
Restart=on-failure
[Install]
WantedBy=multi-user.target
Let’s break down what each section means:
[Unit]
contains metadata about the service, such as its description and dependencies.[Service]
contains information about how the service should run, such as its type, executable path, restart policy, etc.[Install]
contains information about how the service should be enabled or disabled.
You can customize your service file according to your needs. For example, you can add environment variables with Environment
, change the user or group with User
or Group
, limit the resources with CPUQuota
or MemoryLimit
, etc. You can find more options in the official documentation.
Step 2: Reload systemd daemon
After creating your service file, you need to reload the systemd daemon so that it can recognize your new service. You can do this by running:
sudo systemctl daemon-reload
Step 3: Start and enable your service
Now you can start your service by running:
sudo systemctl start my-binary.service
You can replace my-binary.service
with whatever name you gave to your service file.
You can also check the status of your service by running:
sudo systemctl status my-binary.service
You should see something like this:
● my-binary.service - My Binary Service
Loaded: loaded (/etc/systemd/system/my-binary.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2023-03-17 21:59:51 GMT; 10s ago
Main PID: 1234 (my-binary)
Tasks: 5 (limit: 4915)
CGroup: /system.slice/my-binary.service
└─1234 /path/to/my/binary
Mar 17 21:59:51 ubuntu systemd[1]: Started My Binary Service.
Mar 17 21:59:51 ubuntu my-binary[1234]: Hello world!
...
If you want your service to start automatically when the system boots up, you need to enable it by running:
sudo systemctl enable my-binary.service
You can also disable it by running:
sudo systemctl disable my-binary.service
Step 4: Enjoy!
That’s it! You have successfully deployed your binary on Linux as a service using systemd. You can now use commands like systemctl stop
, systemctl restart
, or systemctl reload
to manage your service.
You can also view the logs of your service by running:
journalctl -u my-binary.service
I hope this blog post was helpful for you.