Un script simple de autoinstalación de servicio systemd

Uso esto en repositorios git para facilitar el proceso de despliegue

a-simple-systemd-service-autoinstall-script.sh
#!/bin/bash
# Este script instala y habilita/inicia un servicio systemd
# También instala el archivo de servicio
export NAME=MyService

cat >/etc/systemd/system/${NAME}.service <<EOF
# TODO Copia y pega el archivo .service de systemd aquí
EOF

# Habilitar e iniciar servicio
systemctl enable --now ${NAME}.service

El siguiente ejemplo instala automáticamente un .service de systemd para docker-compose:

a-simple-systemd-service-autoinstall-script-2.sh
#!/bin/bash
# Este script instala y habilita/inicia un servicio systemd
# También instala el archivo de servicio
export NAME=MyService

cat >/etc/systemd/system/${NAME}.service <<EOF
[Unit]
Description=${NAME}
Requires=docker.service
After=docker.service

[Service]
Restart=always
User=root
Group=docker
WorkingDirectory=$(pwd)
# Apagar contenedor (si está corriendo) antes de que la unidad esté siendo iniciada
ExecStartPre=$(which docker-compose) -f docker-compose.yml down
# Iniciar contenedor cuando la unidad es iniciada
ExecStart=$(which docker-compose) -f docker-compose.yml up
# Detener contenedor cuando la unidad es detenida
ExecStop=$(which docker-compose) -f docker-compose.yml down

[Install]
WantedBy=multi-user.target
EOF

# Habilitar e iniciar servicio
systemctl enable --now ${NAME}.service

Echa un vistazo a artículos similares por categoría: Docker Systemd