# Systemd

## Introduction
systemd is a software suite that provides an array of system components for Linux operating systems. Its main aim is to unify service configuration and behavior across Linux distributions; systemd's primary component is a "system and service manager"—an init system used to bootstrap user space and manage user processes.

## Documentation
- [systemd.unit](https://www.freedesktop.org/software/systemd/man/systemd.unit.html)
- [systemd.service](https://www.freedesktop.org/software/systemd/man/systemd.service.html)

## Common Parameters

### Unit

<table>
<tbody>
<thead>
<th>Option</th>
<th>Description</th>
</thead>
<tr>
<td><code>Description</code></td>
<td>A short description of the unit.</td>
</tr>
<tr>
<td><code>Documentation</code></td>
<td>A list of URIs referencing documentation.</td>
</tr>
<tr>
<td><code>Before</code>, <code>After</code></td>
<td>The order in which units are started.</td>
</tr>
<tr>
<td><code>Requires</code></td>
<td>If this unit gets activated, the units listed here will be activated as well. If one of the other units gets deactivated or fails, this unit will be deactivated.</td>
</tr>
<tr>
<td><code>Wants</code></td>
<td>Configures weaker dependencies than <code>Requires</code>. 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. </td>
</tr>
<tr>
<td><code>Conflicts</code></td>
<td>If a unit has a <code>Conflicts</code> setting on another unit, starting the former will stop the latter and vice versa.</td>
</tr>
</tbody>
</table>

Get a complete list of parameters by running `man systemd.unit`

### Install

<table>
<tbody>
<thead>
<th>Option</th>
<th>Description</th>
</thead>
<tr>
<td><code>Alias</code></td>
<td>A space-separated list of additional names for the unit. Most <code>systemctl</code> commands, excluding <code>systemctl enable</code>, can use aliases instead of the actual unit name.</td>
</tr>
<tr>
<td><code>RequiredBy</code>, <code>WantedBy</code></td>
<td>The current service will be started when the listed services are started. See the description of <code>Wants</code> and <code>Requires</code> in the <code>[Unit]</code> section for details.</td>
</tr>
<tr>
<td><code>Also</code></td>
<td>Specifies a list of units to be enabled or disabled along with this unit when a user runs <code>systemctl enable</code> or <code>systemctl disable</code>.</td>
</tr>
</tbody>
</table>

Get a complete list of parameters by running `man systemd.unit`

### Service

<table>
<tbody>
<thead>
<th>Option</th>
<th>Description</th>
</thead>
<tr>
<td><code>Type</code></td>
<td>Configures the process start-up type. One of:<br />
  <ul>
    <li><code>simple</code> (default) – starts the service immediately. It is expected that the main process of the service is defined in <code>ExecStart</code>.</li>
    <li><code>forking</code> – considers the service started up once the process forks and the parent has exited.</li>
    <li><code>oneshot</code> – similar to <code>simple</code>, but it is expected that the process has to exit before systemd starts follow-up units (useful for scripts that do a single job and then exit). You may want to set <code>RemainAfterExit=yes</code> as well so that systemd still considers the service as active after the process has exited.</li>
    <li><code>dbus</code> – similar to <code>simple</code>, but considers the service started up when the main process gains a D-Bus name.</li>
    <li><code>notify</code> – similar to <code>simple</code>, but considers the service started up only after it sends a special signal to systemd.</li>
    <li><code>idle</code> – similar to <code>simple</code>, but the actual execution of the service binary is delayed until all jobs are finished.</li>
  </ul>
</td>
</tr>
<tr>
<td><code>ExecStart</code></td>
<td>Commands with arguments to execute when the service is started. <code>Type=oneshot</code> enables specifying multiple custom commands that are then executed sequentially. <code>ExecStartPre</code> and <code>ExecStartPost</code> specify custom commands to be executed before and after <code>ExecStart</code>.</td>
</tr>
<tr>
<td><code>ExecStop</code></td>
<td>Commands to execute to stop the service started via <code>ExecStart</code>.</td>
</tr>
<tr>
<td><code>ExecReload</code></td>
<td>Commands to execute to trigger a configuration reload in the service.</td>
</tr>
<tr>
<td><code>Restart</code></td>
<td>With this option enabled, the service shall be restarted when the service process exits, is killed, or a timeout is reached with the exception of a normal stop by the <code>systemctl stop</code> command.</td>
</tr>
<tr>
<td><code>RemainAfterExit</code></td>
<td>If set to <code>True</code>, the service is considered active even when all its processes exited. Useful with <code>Type=oneshot</code>. Default value is <code>False</code>.</td>
</tr>
</tbody>
</table>

Get a complete list of parameters by running `man systemd.service`

## Example
```
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
```

```
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[Unit]
Description=Redis persistent key-value database
After=network.target
```

```
[Service]
ExecStart=/usr/bin/redis-server /etc/redis.conf --daemonize no
ExecStop=/usr/bin/redis-shutdown
User=redis
Group=redis

[Install]
WantedBy=multi-user.target
```

[source: shellhacks.com](https://www.shellhacks.com/systemd-service-file-example/)