# Ubuntu Service Creation (Systemd)

Below are instructions on how to setup a binary as a linux service.

For additional systemd commands, see this: <span data-annotation-inline-node="true" data-annotation-mark="true" data-card-url="https://oga.atlassian.net/wiki/spaces/~311198967/pages/89522177" data-inline-card="true" data-renderer-start-pos="113" data-ssr-placeholder="0vDZ-:EfLS5:4y5Pz:qz-Pe:F4Zdx-0"><span data-testid="hover-card-trigger-wrapper">[<span class="_19itglyw _vchhusvi _r06hglyw _o5721jtm _1nmz9jpi _16d9qvcn _ca0qv77o _u5f31b66 _n3tdv77o _19bv1b66" data-testid="inline-card-icon-and-title"><span class="_19itglyw _vchhusvi _r06hglyw">SystemCtl Usage</span></span>](https://oga.atlassian.net/wiki/spaces/~311198967/pages/89522177)</span></span>

### Service Creation

Here are steps to run a binary as a linux service.

Create a systemd unit file for the service by generating a file in: /lib/systemd/system  
The file should contain these things:

```ini
[Unit]
Description=OGA.HostControl.Service
After=network.target

[Service]
ExecStart=/usr/bin/bliss/oga.hostcontrol.service/OGA.HostControl.Service --urls "http://192.168.1.201:4180;http://172.17.0.1:4180"
Environment=DOTNET_CLI_HOME=/temp
WorkingDirectory=/usr/bin/bliss/oga.hostcontrol.service
Restart=on-failure

[Install]
WantedBy=multi-user.target
```

<p class="callout info">NOTE: The above systemd unit file was generated for a dotnet service, called: OGA.HostControl.Service.</p>

<p class="callout info">NOTE: This service runs as root, as no user was specified in the \[Service\] section.</p>

<p class="callout info">NOTE: We have specified a working directory that points back to the bin folder of the service. This is necessary for a dotnet service.</p>

Once the unit file has been created for the service, you must tell systemd to load it.  
Tell systemd to reload unit files with this:

```bash
sudo systemctl daemon-reload
```

With the unit file available, you should be able to start the service.

Start the service with this:

```bash
sudo systemctl start servicename
```

Verify the service is running with this:

```bash
sudo systemctl status servicename
```

Once happy that the service is configured correctly and will run as required, you should enable it to start on boot.

To enable a service to start on boot, do this:

```bash
sudo systemctl enable servicename
```

At this point, your service will startup each time the machine reboots.