journalctl
Managing Services
systemctl start name: Start a service.
systemctl enable name: Enable a service.
systemctl stop name: Stop a service.
systemctl disable name: Disable a service.
systemctl restart name: Restart a service.
systemctl try-restart name: Restart a service if it is running.
systemctl reload name: Reload a service's configuration.
systemctl status name: Check if service(s) are running.
Faster Shutdown
When a daemon refuses to shut down immediately, on systemd based systems, things
that don't shut down trigger a default 90 second timeout. This value can be
changed in system.conf with the DefaultTimeoutStopSec option.
Creating Custom Unit Files
For running your own custom daemons or secondary instances of existing services.
Install the program you'll be running as a daemon.
Create a unit file in /etc/systemd/system:
touch /etc/systemd/system/name.service
chmod 664 /etc/systemd/system/name.service
Open the name.service file and add some configuration options:
[Unit]
Description=service_description
After=network.target
[Service]
ExecStart=path_to_executable
Type=forking
PIDFile=path_to_pidfile
[Install]
WantedBy=default.target
Notify systemd that a new name.service file exists:
systemctl daemon-reload
systemctl start name.service
Unit section syntax: more in systemd.unit(5)
Description
A meaningful description of the unit. This text is displayed for example in the output of the systemctl status command.
Documentation
Provides a list of URIs referencing documentation for the unit.
After
Defines the order in which units are started. The unit starts only after the units specified in After are active. Unlike Requires, After does not explicitly activate the specified units. The Before option has the opposite functionality to After.
Requires
Configures dependencies on other units. The units listed in Requires are activated together with the unit. If any of the required units fail to start, the unit is not activated.
Wants
Configures weaker dependencies than Requires. If any of the listed units does not start successfully, it has no impact on the unit activation. This is the recommended way to establish custom unit dependencies.
Conflicts
Configures negative dependencies, an opposite to Requires.
Service section syntax
Type
Configures the unit process startup type that affects the functionality of ExecStart and related options. One of:
- simple – The default value. The process started with ExecStart is the main process of the service.
- forking – The process started with ExecStart spawns a child process that becomes the main process of the service. The parent process exits when the startup is complete.
- oneshot – This type is similar to simple, but the process exits before starting consequent units.
- dbus – This type is similar to simple, but consequent units are started only after the main process gains a D-Bus name.
- notify – This type is similar to simple, but consequent units are started only after a notification message is sent via the sd_notify() function.
- idle – similar to simple, the actual execution of the service binary is delayed until all jobs are finished, which avoids mixing the status output with shell output of services.
ExecStart
Specifies commands or scripts to be executed when the unit is started. ExecStartPre and ExecStartPost specify custom commands to be executed before and after ExecStart. Type=oneshot enables specifying multiple custom commands that are then executed sequentially.
ExecStop
Specifies commands or scripts to be executed when the unit is stopped.
ExecReload
Specifies commands or scripts to be executed when the unit is reloaded.
Restart
With this option enabled, the service is restarted after its process exits, with the exception of a clean stop by the systemctl command.
RemainAfterExit
If set to True, the service is considered active even when all its processes exited. Default value is False. This option is especially useful if Type=oneshot is configured.
User
User to run the service with.
Group
Group to run the service with.
Install section syntax
Alias
Provides a space-separated list of additional names for the unit. Most systemctl commands, excluding systemctl enable, can use aliases instead of the actual unit name.
RequiredBy
A list of units that depend on the unit. When this unit is enabled, the units listed in RequiredBy gain a Require dependency on the unit.
WantedBy
A list of units that weakly depend on the unit. When this unit is enabled, the units listed in WantedBy gain a Want dependency on the unit.
Also
Specifies a list of units to be installed or uninstalled along with the unit.
DefaultInstance
Limited to instantiated units, this option specifies the default instance for which the unit is enabled.