GNU/Linux >> Tutoriales Linux >  >> Cent OS

Cómo agregar un script personalizado a systemd en CentOS/RHEL 7

La función systemd reemplaza los antiguos scripts de inicialización de System-V de versiones anteriores. systemd es una instalación impulsada por eventos que permite iniciar, controlar o detener subsistemas no dependientes en paralelo. Aquí explicamos cómo agregar un script personalizado a la función systemd.

1. Escribir y depurar el script personalizado

Por lo general, un script systemd se escribe como un script de shell. Comience escribiendo su script personalizado usando las convenciones normales. Llamaremos a nuestro script my-custom-script.sh y es sencillo:

#!/bin/sh
echo I am a custom script

2. El script debe ser ejecutable

Hagamos que el script sea ejecutable:

# chmod 0755 my-custom-script.sh

3. Describa el script personalizado para systemd

Con el script escrito y probado manualmente, el script está listo para ser descrito en el sistema systemd. Para hacer esto, se necesita un archivo [nombre].servicio. La sintaxis usa el formato INI comúnmente usado para archivos de configuración. Continuando con nuestro ejemplo, necesitamos un archivo my-custom-script.service. El ejecutable se ejecutará exactamente una vez por cada vez que se inicie el servicio. El servicio no se iniciará hasta que la capa de red esté activa y estable:

# This is my-custom-script.service, which describes the my-custom-script.sh file
[Unit]
Description=This is executed on shutdown or reboot
DefaultDependencies=no
Wants=network-pre.target                                                                   # (if network is required before running the script)
Before=network-pre.target shutdown.target reboot.target halt.target                        # Defines the order in which units are stoped. #(REQUIRED)

[Service]
Type=oneshot                                                                               # enables specifying multiple custom commands that are then executed sequentially. (REQUIRED)
RemainAfterExit=true                                                                       # required by the oneshot setting (REQUIRED)
Environment=ONE='one' "TWO='2"                                                             # you can set some environment variables, that may be necessary to pass as arguments
ExecStart=/bin/true                                                                        # because is a shutdown script nothing is done when this service is started
ExecStop=/bin/bash /usr/local/bin/my-custom-script.sh ${ONE} ${TWO}                        # < --*********** change to the script full path ************ (REQUIRED)
TimeoutStopSec=1min 35s                                                                    # Configures the time to wait for stop.

[Install]
WantedBy=multi-user.target                                                                 # When this unit is enabled, the units listed in WantedBy gain a Want dependency on the unit. (REQUIRED)

4. Coloque el archivo de servicio en el directorio de recopilación de servicios esperado

Coloquemos el script personalizado en el directorio de recopilación de servicios, es decir, /etc/systemd/system/ :

# cp my-custom-script.sh /etc/systemd/system/

5. Habilite el script para futuros reinicios

Similar al chkconfig de versiones anteriores, el servicio debe estar habilitado. Dado que se agregó un nuevo servicio, notifique al demonio systemd que se reconfigure:

# systemctl enable my-custom-script.service
# systemctl daemon-reload


Cent OS
  1. cómo agregar un script personalizado a systemd en CentOS/RHEL 7

  2. Cómo agregar/eliminar un archivo de configuración regional en CentOS/RHEL 5,6

  3. CentOS / RHEL:Cómo agregar un archivo de intercambio

  4. CentOS / RHEL:Cómo agregar reglas de iptable

  5. Cómo agregar una configuración regional en CentOS/RHEL 8

Cómo instalar Dropbox en RHEL 8 / CentOS 8

Cómo agregar un usuario a un grupo en RHEL 8 / CentOS 8

Cómo instalar la brújula en RHEL 8 / CentOS 8

Cómo instalar Puppet en RHEL 8 / CentOS 8

Cómo instalar/actualizar Systemd en RHEL/CentOS 7

CentOS / RHEL:Cómo agregar una nueva partición de intercambio