# How to setup an SSH Monitor daemon

Running an {{IOC_program}} as a
[daemon](https://en.wikipedia.org/wiki/Daemon_(computing)) is really convenient in production. It's
very useful - among other things - in order to log the {{IOC_shell}},
in order to allow multiple users to share the
same {{IOC_shell}} screen at the same time, in order to
access and leave the {{IOC_shell}} at any point in time
without having to restart the {{IOC_program}}, etc.

If you wonder how to daemonize an {{IOC_program}}, here are
two suggestions with `procServ` and `tmux`.

---

## `procServ` daemon

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)

* Install the [`procServ` package](https://repology.org/project/procserv/versions) on your
  {{host}}'s Linux distribution:
  * E.g. with `apt`: `$ sudo apt install procserv`
  * E.g. with `yum`: `$ sudo yum install procserv`
  * E.g. with `dnf`: `$ sudo dnf install procserv`

`procServ` can be used as an easy daemonizer,
e.g. with the {{SSH_Monitor}} {{IOC_program}}
used as an example in the [`.cmd` and `.substitutions` how-to](./cmd_and_sub_how_to.md):

```{code} bash
$ procServ -n "sshmonitorTarget1" -c /path/to/myTargetMonitoringTop/iocBoot/iocMyTargetMonitoring/ -i ^D^C 20000 ./st_target1.cmd
```

```{important}
With the above command,
the `procServ` instance associated to the {{IOC_program}} is attached to the TCP port `20000`.
You want to make sure to use different and unique TCP port numbers
for each new `procServ` instance associated to other {{IOC_program}}s
(that are run in parallel).
```

## `procServ` tips

* Find `procServ` PID:

    ```{code} bash
    $ pgrep procServ
    $ ps -aux | grep "procServ"
    ```

* Find the {{SSH_Monitor}} {{IOC_program}} (running inside `procServ`) PID:

    ```{code} bash
    $ pgrep sshmonitorTarget1
    $ ps -aux | grep "sshmonitorTarget1"
    ```

* Restart the {{SSH_Monitor}} {{IOC_program}} (running inside `procServ`) by
  just killing it: `procServ` will restart it automatically:

    ```{code} bash
    $ pgrep sshmonitorTarget1
        > 123123
    $ sudo kill -9 123123
    ```

* Connect locally to `procServ`:

    ```{code} bash
    $ telnet 127.0.0.1 20000
    ```

* Quit `procServ` - without stopping the {{IOC_program}} - by entering
  `Ctrl + AltGr + ]` and then enter `quit`.

* Connect remotely to `procServ`:

    ```{code} bash
    $ ssh host-username@<host-ip-address> -t telnet 20000
    ```

```{seealso}
For more details about `procServ`, see <https://github.com/ralphlange/procServ>
```

---

## `tmux` daemon

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)

* Install the [`tmux` package](https://repology.org/project/tmux/versions) on your Linux
  distribution:
  * E.g. with `emerge`: `$ sudo emerge -a app-misc/tmux`
  * E.g. with `pacman`: `$ sudo pacman -S tmux`
  * E.g. with `apt`: `$ sudo apt install tmux`
  * E.g. with `yum`: `$ sudo yum install tmux`
  * E.g. with `dnf`: `$ sudo dnf install tmux`

`tmux` can also be used as an easy daemonizer,
e.g. with the {{SSH_Monitor}} {{IOC_program}}
used as an example in the [`.cmd` and `.substitutions` how-to](./cmd_and_sub_how_to.md):

```{code} bash
$ tmux new-session -d -s sshmonitorTarget1 'cd /path/to/myTargetMonitoringTop/iocBoot/iocsshmonitor && ./st_target1.cmd'
```

## `tmux` tips

* Enter your `sshmonitorTarget1` `tmux` session (i.e. your {SSH_Monitor}} {{IOC_shell}} for `target1`):

    ```{code} bash
    $ tmux a -t sshmonitorTarget1
    ```

  * Help screen (Q to quit):

      ```{code} bash
      Ctrl+b ?
      ```
  
  * Scroll in window:

      ```{code} bash
      Ctrl+b PageUp/PageDown
      ```
  
  * Enter scroll mode:

      ```{code} bash
      Ctrl+b [
      ```

      > (then up/down arrow keys and/or pageup/pagedown)
  
  * Exit scroll mode:

      ```{code} bash
      > q
      ```
  
  * Detach from `tmux` without stopping the {{IOC_program}}:

    ```{code} bash
    Ctrl+b d
    ```

* Stop the `tmux` daemon and associated {{IOC_program}}:

    ```{code} bash
    $ tmux kill-session -t sshmonitorTarget1
    ```

* List currently running `tmux` daemons:

    ```{code} bash
    $ tmux ls # or `$ tmux list-sessions`
    ```

```{seealso}
For more details about `tmux`, see:

* <https://github.com/tmux/tmux/wiki>
* <https://wiki.archlinux.org/title/Tmux>
* <https://wiki.gentoo.org/wiki/Tmux>
```
