Run any Executable as a Service in Linux
Running any executable as a linux service
Here’s a set of instructions on How to Configure to Run any Executable as a Service in Linux with Systemd.
Above is an AI-generated image (by Flux 1 dev model) of the cogs representing linux DIY approach.
Systemd
Systemd is a system and services manager and initialization tool in some linux distros.
Some of the popular distros with systemd:
- Ubuntu 16.04 or newer
- Debian Jesse or newer
- Arch Linux
- CentOS 7 / RHEL 7 or newer
- Fedora 15 or newer
systemctl
systemctl
is a commandline tool to control the systemd
.
list all services
To list of all services run
systemctl list-units --type=service
Create a service file
Navigate to the systemd service directory and create a new file with a .service extension13:
sudo nano /etc/systemd/system/your_service_name.service
Configure the service file:
Add the following content to the file, adjusting the values as needed135:
[Unit]
Description=Your Service Description
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/path/to/your/executable/directory
ExecStart=/path/to/your/executable
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Save and close the file.
Delayed service start
If you suspect a timing issue during service start, consider adding a delay to your service config:
[Service]
ExecStartPre=/bin/sleep 10
Service dependencies
To configure your service dependencies: in the the [Unit] section of the service config gile file,
add the following directives:
Use After= to specify services that should start before your service:
After=network.target other_service.service
Use Requires= to define hard dependencies:
Requires=required_service.service
Use Wants= for soft dependencies:
Wants=optional_service.service
Reload systemd to recognize the new service
sudo systemctl daemon-reload
Check service dependencies
sudo systemctl list-dependencies your_service_name.service
Enable the service to start on boot
sudo systemctl enable your_service_name.service
To verify that your service is set to start on boot:
sudo systemctl is-enabled your_service_name.service
Start the service
sudo systemctl start your_service_name.service
# or
sudo systemctl restart your_service_name.service
Check the service status
If something went wrong - check linux service logs and status:
sudo systemctl status your_service_name.service
or
sudo journalctl -u your_service_name.service
For Python Scripts
ExecStart=/path/to/conda/envs/my_env_name/bin/python /path/to/executable.py
Comments
You can now manage your service using systemctl commands, such as start
, stop
, restart
, and status
.
For executables that require parameters, modify the ExecStart
line in the service file accordingly.
For example If you need to run a Java application, use the full Java command in the ExecStart line.
Remember to adjust file permissions and ownership as necessary, and ensure that the executable has the correct execute permissions.
Systemd services control with chkservice
Instead of executing
sudo systemctl start some-serviceservice
# or
sudo systemctl restart some-serviceservice
You can use console UI tool. easy and lightweight
sudo apt-get install chkservice
sudo chkservice