# Systemd

```
vim /etc/systemd/system/foo.service
chmod +x /etc/systemd/system/foo.service
```

> \[Unit\]   
> Description=foo   
>   
> \[Service\]   
> ExecStart=/bin/bash echo "Hello World!"   
>   
> \[Install\]   
> WantedBy=multi-user.target

```
systemctl daemon-reload
```

```
systemctl start foo
```

You can also use systemctl cat nginx.service to simply view how the init script starts the service

To enable and start a service in the same line you can do

```
systemctl enable --now foo.service
```

To check if service is enabled

```
systemctl is-enabled foo.service; echo $?
```

To check the services that start with the OS in order you can do

```
systemctl list-units --type=target
```

[![gnome-shell-screenshot-DIY83Y.png](https://wiki.myhypervisor.ca/uploads/images/gallery/2017-12-Dec/scaled-840-0/gnome-shell-screenshot-DIY83Y.png)](https://wiki.myhypervisor.ca/uploads/images/gallery/2017-12-Dec/gnome-shell-screenshot-DIY83Y.png)

### Journalctl

List failed services

```shell
systemctl --failed
```

```shell
journalctl -p 3 -xb
```

To filter only 1 service you will need to use the flag -u

```
journalctl -u nginx.service
```

To have live logs on a service you can do

```
journalctl -f _SYSTEMD_UNIT=nginx.service
```

To have live-tail logs for 2 services example nginx and ssh

```
journalctl -f _SYSTEMD_UNIT=nginx.service + _SYSTEMD_UNIT=sshd.service
```

To check logs since the latest boot:

```
journalctl -b
```

To get the data from yesterday

```
journalctl --since yesterday
#or
journalctl -u nginx.service --since yesterday
```

To view kernel messages

```
journalctl -k
```