# How to setup an SSH Monitor service

If you want to automatically start your {{SSH_Monitor}} {{IOC_program}} at boot time,
you might want to create a system service **either** with `systemd`, `OpenRC`, `Runit` or `SysVinit`
(depending on your init system).

---

## `systemd` service how-to

Prerequisites:

* [Installation how-to](./install_update_remove_how_to.md#how-to-install).
* [Firewall how-to for SSH](./firewall_how_to.md#firewall-configuration-for-ssh).
* [Firewall how-to for EPICS](./firewall_how_to.md#standard-firewall-configuration-for-epics).
* [SSH how-to](./ssh_how_to.md).
* [`.cmd` and `.substitutions` how-to](./cmd_and_sub_how_to.md)
* [Security how-to](./security_how_to.md)
* [Run SSH Monitor how-to](./run_ssh_monitor_how_to.md)
* [Custom shell instructions how-to](./custom_shell_instructions_how_to.md)
* [EPICS records' fields how-to](./epics_records_fields_how_to.md)
* [`procserv` daemon how-to](./ssh_monitor_daemon_how_to.md#procserv-daemon)

On the {{host}}, you can create a systemd service like so (e.g. with
[procServ](./ssh_monitor_daemon_how_to.md#procserv-daemon)):

```{code} bash
$ vi /usr/lib/systemd/system/myTargetMonitoring.service
  + > [Install]
  + > WantedBy=multi-user.target
  + >
  + > [Unit]
  + > After=network-online.target
  + > Wants=network-online.target
  + > Description=EPICS IOC monitoring target1
  + >
  + > [Service]
  + > ExecStart=procServ '--chdir' '/path/to/myTargetMonitoringTop/iocBoot/iocMyTargetMonitoring' '--foreground' '--logfile' '-' '--oneshot' 2000 '/path/to/myTargetMonitoringTop/iocBoot/iocMyTargetMonitoring/st_target1.cmd'
  + > Restart=on-failure
  + > DynamicUser=yes
  + > StateDirectory=epics/myTargetMonitoring

$ systemctl enable myTargetMonitoring
```

---

## `OpenRC` service how-to

🚧 **TODO** (help is welcome / appreciated) 🚧

---

## `Runit` service how-to

Prerequisites:

* [Installation how-to](./install_update_remove_how_to.md#how-to-install).
* [Firewall how-to for SSH](./firewall_how_to.md#firewall-configuration-for-ssh).
* [Firewall how-to for EPICS](./firewall_how_to.md#standard-firewall-configuration-for-epics).
* [SSH how-to](./ssh_how_to.md).
* [`.cmd` and `.substitutions` how-to](./cmd_and_sub_how_to.md)
* [Security how-to](./security_how_to.md)
* [Run SSH Monitor how-to](./run_ssh_monitor_how_to.md)
* [Custom instructions how-to](./custom_shell_instructions_how_to.md)
* [EPICS records' fields how-to](./epics_records_fields_how_to.md)
* [`procserv` daemon how-to](./ssh_monitor_daemon_how_to.md#procserv-daemon)

---

* Most distros using Runit won't store *available* services in the same directory. So let's define
  a `$AVAILABLE_RUNIT_SERVICE_DIR` environment variable holding the path to that directory. Most
  common paths are:
  * `/etc/sv` (e.g. for Void Linux)
  * `/etc/runit/`
  * `/etc/runit/sv` (e.g. for Artix Linux)

* Most distros using Runit won't store *active* services in the same directory. So let's define a
  `$ACTIVE_RUNIT_SERVICE_DIR` environment variable holding the path to that directory. Most common
  paths are:
  * `/service/`
  * `/var/service/` (e.g. for Void Linux)
  * `/etc/service/`
  * `/run/runit/service/` (e.g. for Artix Linux)

* Create the active `myTargetMonitoring` service directory:

    ```{code} bash
    # mkdir $AVAILABLE_RUNIT_SERVICE_DIR/myTargetMonitoring/
    ```

* Create a down file in the service directory, until the setup is completed:

    ```{code} bash
    # touch $AVAILABLE_RUNIT_SERVICE_DIR/myTargetMonitoring/down
    ```

* Create a run file in the service directory:

    ```{code} bash
    # touch $AVAILABLE_RUNIT_SERVICE_DIR/myTargetMonitoring/run
    # chmod +x /service/service/myTargetMonitoring/run
    # vi /service/service/myTargetMonitoring/run
      > #!/bin/sh
      > exec 2>&1 # redirect output of stderr to stdout
      >
      > exec cd myTargetMonitoringTop/iocBoot/iocMyTargetMonitoring && ./st_target1.cmd
      >
      > # or e.g. with tmux:
      > # exec tmux new-session -d -s myTargetMonitoringTarget1 'cd /path/to/myTargetMonitoringTop/iocBoot/iocMyTargetMonitoring && ./st_target1.cmd'
      >
      > # or e.g. with procServ:
      > # exec procServ -n "myTargetMonitoringTarget1" -c /path/to/your/myTargetMonitoringTop/iocBoot/iocMyTargetMonitoring -i ^D^C 20000 ./st_target1.cmd
    ```

* Enable logging:

    ```{code} bash
    # mkdir $AVAILABLE_RUNIT_SERVICE_DIR/myTargetMonitoring/log/
    # touch $AVAILABLE_RUNIT_SERVICE_DIR/myTargetMonitoring/log/run
    # chmod +x $AVAILABLE_RUNIT_SERVICE_DIR/log/run
    $ vi $AVAILABLE_RUNIT_SERVICE_DIR/log/run
      > #!/bin/sh
      > exec svlogd -tt .
    ```

* Enable `myTargetMonitoring` and start it:

    ```{code} bash
    # ln -s $AVAILABLE_RUNIT_SERVICE_DIR/myTargetMonitoring $ACTIVE_RUNIT_SERVICE_DIR
    # sv start myTargetMonitoring
    ```

---

## `SysVinit` service how-to

🚧 **TODO** (help is welcome / appreciated) 🚧
