# Ejecutar como un servicio

## Archivo `env`

Creamos un shell script que luego utilizaremos para arrancar el nodo usando `systemctl`.

{% tabs %}
{% tab title="RELAY" %}
{% code title="\~/cardano-node/relay/env" %}

```
TOPOLOGY=/home/ubuntu/cardano-node/relay/topology.json
DATABASE_PATH=/home/ubuntu/cardano-node/relay/db
SOCKET_PATH=/home/ubuntu/cardano-node/relay/db/socket
HOST_ADDR=0.0.0.0
PORT=3001
CONFIG=/home/ubuntu/cardano-node/relay/config.json
```

{% endcode %}
{% endtab %}

{% tab title="BLOCK" %}
{% code title="\~/cardano-node/block/env" %}

```
TOPOLOGY=/home/ubuntu/cardano-node/block/topology.json
DATABASE_PATH=/home/ubuntu/cardano-node/block/db
SOCKET_PATH=/home/ubuntu/cardano-node/block/db/socket
HOST_ADDR=0.0.0.0
PORT=3000
CONFIG=/home/ubuntu/cardano-node/block/config.json
SHELLEY_KES_KEY=/home/ubuntu/cardano-node/block/BP.kes.skey
SHELLEY_VRF_KEY=/home/ubuntu/cardano-node/block/BP.vrf.skey
SHELLEY_OPCERT=/home/ubuntu/cardano-node/block/BP.node.opcert
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Archivo de servicio

Creamos un archivo de servicio para poder programar y administrar el servicio usando `systemctl`. Debes iniciar tu editor de texto con `sudo` para poder guardar los cambios a estos archivos.

{% hint style="warning" %}
No usar el servicio para iniciar nuestro BP hasta después de haber generado nuestras llaves y registrado el pool. Hasta entonces se debe iniciar el nodo como un relay normal. Se incluye un un script "TEMP" como ejemplo.
{% endhint %}

{% tabs %}
{% tab title="RELAY" %}
{% code title="/etc/systemd/system/RELAY.service" %}

```
[Unit]
Description=Cardano Haskell Node - RELAY
After=syslog.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=5
User=ubuntu
LimitNOFILE=131072
WorkingDirectory=/home/ubuntu/cardano-node/relay
EnvironmentFile=/home/ubuntu/cardano-node/relay/env
ExecStart=/home/ubuntu/.local/bin/cardano-node \
  +RTS -N -RTS run \
  --topology ${TOPOLOGY} \
  --database-path ${DATABASE_PATH} \
  --socket-path ${SOCKET_PATH} \
  --host-addr ${HOST_ADDR} \
  --port ${PORT} \
  --config ${CONFIG}
KillSignal=SIGINT
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=RELAY

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

{% endcode %}
{% endtab %}

{% tab title="BLOCK" %}
{% code title="/etc/systemd/system/BLOCK.service" %}

```
[Unit]
Description=Cardano Haskell Node - BLOCK
After=syslog.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=5
User=ubuntu
LimitNOFILE=131072
WorkingDirectory=/home/ubuntu/cardano-node/block
EnvironmentFile=/home/ubuntu/cardano-node/block/env
ExecStart=/home/ubuntu/.local/bin/cardano-node \
  +RTS -N -RTS run \
  --topology ${TOPOLOGY} \
  --database-path ${DATABASE_PATH} \
  --socket-path ${SOCKET_PATH} \
  --host-addr ${HOST_ADDR} \
  --port ${PORT} \
  --config ${CONFIG} \
  --shelley-kes-key ${SHELLEY_KES_KEY} \
  --shelley-vrf-key ${SHELLEY_VRF_KEY} \
  --shelley-operational-certificate ${SHELLEY_OPCERT}
KillSignal=SIGINT
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=BLOCK

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

{% endcode %}
{% endtab %}

{% tab title="TEMP" %}
{% code title="/etc/systemd/system/BLOCK.service" %}

```
[Unit]
Description=Cardano Haskell Node - BLOCK
After=syslog.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=5
User=ubuntu
LimitNOFILE=131072
WorkingDirectory=/home/ubuntu/cardano-node/block
EnvironmentFile=/home/ubuntu/cardano-node/block/env
ExecStart=/home/ubuntu/.local/bin/cardano-node \
  +RTS -N -RTS run \
  --topology ${TOPOLOGY} \
  --database-path ${DATABASE_PATH} \
  --socket-path ${SOCKET_PATH} \
  --host-addr ${HOST_ADDR} \
  --port ${PORT} \
  --config ${CONFIG}
KillSignal=SIGINT
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=BLOCK

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

{% endcode %}
{% endtab %}
{% endtabs %}

## Administrar nuestro servicio

Podemos usar los siguientes comandos para administrar nuestro servicio de cardano-node.

{% tabs %}
{% tab title="RELAY" %}

```
# Habilitar arranque automatico del nodo al encender el servidor 
sudo systemctl enable RELAY

# Habilitar arranque automatico e iniciar servicio inmediatamente (opcional)
sudo systemctl enable --now RELAY

# Iniciar servicio
sudo systemctl start RELAY

# Detener servicio
sudo systemctl stop RELAY

# Ver estado del servicio
sudo systemctl status RELAY
```

{% endtab %}

{% tab title="BLOCK" %}

```
# Habilitar arranque automatico del nodo al encender el servidor 
sudo systemctl enable BLOCK

# Habilitar arranque automatico e iniciar servicio inmediatamente (opcional)
sudo systemctl enable --now BLOCK

# Iniciar servicio
sudo systemctl start BLOCK

# Detener servicio
sudo systemctl stop BLOCK

# Ver estado del servicio
sudo systemctl status BLOCK
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://es-kb.topopool.com/primeros-pasos/ejecutar-servicio.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
