Creating a service in Ubuntu to run a Docker image
Systemd is a system and service manager for Linux operating systems. It is responsible for managing the services and daemons running on the system. Systemd uses unit files to define and manage services in the directory
Create a file in
[Unit] Description=Onion Share Wants=network-online.target After=network-online.target Wants=docker.service After=docker.service StartLimitInterval=0 [Service] Type=forking TimeoutStartSec=infinity TimeoutStopSec=16min ExecStart=/mnt/disk1/onion-farm/scripts/start ExecStop=/mnt/disk1/umbrel/scripts/stop User=root Group=root StandardOutput=syslog StandardError=syslog SyslogIdentifier=onion-service-startup RemainAfterExit=yes Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
A summary of the fields that can be used in this file follows.
The [Unit] section is used to define metadata about the unit. It contains options such as Description, After, Before, Requires, Wants, and Conflicts.
The [Service] section is used to configure the service that the unit file describes. It contains options such as Type, ExecStart, User, Group, WorkingDirectory, Restart, TimeoutSec, and RestartSec.
The [Install] section is used to specify installation-related options for the unit file. It contains options such as WantedBy and RequiredBy.
The Description option is used to provide a human-readable description of the unit. It should briefly describe what the unit does.
The After option specifies the units that must be started before this unit. This is useful when the current unit depends on other units being already started.
The Before option specifies the units that must be started after this unit. This is useful when other units depend on the current unit being already started.
The Requires option specifies that the specified unit must be started before this unit. If the specified unit fails to start, this unit will also fail.
The Wants option is similar to the Requires option, but the specified unit is not necessary for the current unit to function.
The Conflicts option specifies units that are not allowed to be run at the same time as the current unit. If a conflicting unit is already running, the current unit will not be started.
The Type option specifies how the service should be started. The possible values are simple, forking, oneshot, dbus, notify, idle, and watchdog.
The ExecStart option specifies the command that should be executed to start the service. This option can include arguments, and multiple commands can be specified.
The User option specifies the user account under which the service should be run.
The Group option specifies the group under which the service should be run.
The WorkingDirectory option specifies the working directory for the service.
The Restart option specifies when the service should be restarted. The possible values are no, always, on-success, on-failure, on-abnormal, on-watchdog, on-abort, and on-disconnect.
The TimeoutSec option specifies the time in seconds that systemd will wait for the service to start. If the service does not start in the specified time, systemd will consider it to have failed.
The RestartSec option specifies the time in seconds that systemd should wait before restarting the service after it has stopped.
The WantedBy option specifies the target that should be enabled when the unit is installed.
The RequiredBy option specifies the target that should be enabled when the unit is installed. This is similar to WantedBy, but the target is required rather than just being wanted.
In conclusion, understanding the parameters in a systemd unit file is essential to manage services and daemons effectively on Linux operating systems. Knowing how to use these parameters can help you configure services to start at boot time, restart automatically, and recover from errors.
Create a directory where the files will be created. In my case will be
#!/bin/bash docker run \ -p 3000:5000 \ -v $(pwd)/html/:/var/www/html/ \ -v $(pwd)/http.d/:/etc/nginx/http.d/ \ -v $(pwd)/torrc.d/:/etc/torrc.d/ \ -v $(pwd)/tor/:/var/lib/tor/ \ -d \ --name onion-farm \ ricardobchaves6/onion-farm:v1.0.0
#!/bin/bash docker stop -t 30 onion-farm docker rm onion-farm
Add the correct permission to files.
sudo chmod 775 start stop
Execute the above command to initiate the service in a startup Ubuntu.
sudo systemctl enable onion-farm-startup.service
And now, test your service:
systemctl start onion-farm-startup.service systemctl stop onion-farm-startup.service
February 19, 2023
Background image credits forSigmund