Adding a New Systemd Service
To create a custom service that is managed by systemd, you must create a service unit file and define its configuration.
Step 1: Create the Service File
Create a new service file
sudo vim /etc/systemd/system/[service_name].serviceCreate a new file with the `.service` extension in the `/etc/systemd/system/` directory.
Step 2: Add the Configuration
The service file uses an INI-style syntax with sections like `[Unit]`, `[Service]`, and `[Install]`.
[Unit]
Description=Description of the service
After=network.target
[Service]
ExecStart=/path/to/your/application
Restart=always
User=your_username
[Install]
WantedBy=multi-user.target
Key Configuration Options
- `Description`: A brief description of the service.
- `After`: Specifies that this service should start after the `network.target` unit is loaded.
- `ExecStart`: The full path to the executable or script that starts the service.
- `Restart`: Defines when the service should be restarted (e.g., `always`, `on-failure`).
- `User`: The user that the service will run as.
- `WantedBy`: The target that will start this service (e.g., `multi-user.target` for a multi-user environment).
Step 3: Reload and Enable
Reload the systemd configuration
sudo systemctl daemon-reloadThis command tells systemd to reload its configuration files, making your new service file available.
Enable and start the service
sudo systemctl enable [service_name]Enables your new service to start on boot.
sudo systemctl start [service_name]Starts the service immediately.