Control Systemd Services through Web or REST API with Sysdweb
Are you tired of manually controlling systemd services on your Linux system? Look no further than Sysdweb, a powerful tool that allows you to control systemd services through a web interface or REST API. Whether you need to start, stop, restart, reload or get the status of a service, Sysdweb has got you covered.
Installation
To get started with Sysdweb, you’ll need to install a few prerequisites. For Ubuntu/Debian users, the libsystemd-dev
package is required. On CentOS, you’ll need to install the systemd-devel
package. Once you have the prerequisites installed, you can choose to install Sysdweb either from source or from PyPI.
From source
If you prefer to install from source, you can clone the Sysdweb repository and install it using pip
. Here are the steps to follow:
#sh
git clone https://github.com/ogarcia/sysdweb.git
virtualenv3 ./sysdweb-venv
source ./sysdweb-venv/bin/activate
cd sysdweb
pip install .
From PyPI
Alternatively, you can install Sysdweb from PyPI using pip
. Here are the steps to follow:
#sh
virtualenv3 ./sysdweb-venv
source ./sysdweb-venv/bin/activate
pip install sysdweb
For Arch Linux users
If you are an Arch Linux user, you can install Sysdweb from the Arch User Repository (AUR).
Configuration
Before running Sysdweb, you’ll need to configure it by editing the sysdweb.conf
file. This file allows you to specify the listen port and address for Sysdweb. You can place the sysdweb.conf
file in different locations depending on your needs. For system-wide configuration, place it in /etc
. For user-specific configuration, place it in ~/.config/sysdweb/sysdweb.conf
. Alternatively, you can place it in the same directory where you run Sysdweb.
Running Sysdweb
Once you have configured Sysdweb, you can start it by running the following command:
#
sysdweb
By default, Sysdweb listens on port 10088 and the address 127.0.0.1. If you want to change the listen port and address, you can use the -p
and -l
options, or specify them as environment variables. For example:
#sh
sysdweb -p 9080 -l 0.0.0.0
Using the Sysdweb API
Sysdweb provides a REST API that allows you to control configured services. The API endpoint is /api/v1/<service>/<action>
and always uses the GET
method. The response from the API is in JSON format and contains the result of the action.
To control a service, you need to specify the <service>
and <action>
in the API endpoint. The <service>
is defined in the config file and corresponds to a section label. For example, if you have a service defined as follows in the config file:
#
[ngx]
title = Nginx
unit = nginx.service
The API endpoint for controlling this service would be /api/v1/ngx/<action>
.
The possible <actions>
that you can perform on a service are:
- start
- stop
- restart
- reload
- reloadorrestart
- status
- journal
All actions (except status
and journal
) return OK
as the result if they can communicate with DBUS. Otherwise, they return Fail
if any error occurs.
For the status
action, the possible responses are:
- active (started unit)
- reloading
- inactive (stopped unit)
- failed (stopped unit)
- activating
- deactivating
- not-found (for non-existent unit)
By default, the /api/v1/<service>/journal
endpoint returns the last 100 lines of the service’s journal file. You can specify the number of lines by appending it to the endpoint, for example: /api/v1/<service>/journal/200
.
Here are some examples of valid API endpoints:
- http://127.0.0.1:10088/api/v1/ngx/start
- http://127.0.0.1:10088/api/v1/ngx/stop
- http://127.0.0.1:10088/api/v1/ngx/restart
- http://127.0.0.1:10088/api/v1/ngx/reload
- http://127.0.0.1:10088/api/v1/ngx/reloadorrestart
- http://127.0.0.1:10088/api/v1/ngx/status
- http://127.0.0.1:10088/api/v1/ngx/journal
- http://127.0.0.1:10088/api/v1/ngx/journal/200
That’s it! You now know how to install Sysdweb, configure it, and use the API to control systemd services. Have fun automating your service management tasks with Sysdweb!
If you have any questions or need further assistance, feel free to leave a comment below.
References
- Sysdweb repository: Sysdweb
- AUR package for Arch Linux users: Sysdweb on AUR
Blake Bradford is a Senior Software Engineer with experience in building scalable and performant software systems. He enjoys sharing his knowledge with fellow engineers and solution architects through his articles and tutorials.
Leave a Reply