Este artículo explica cómo agregar y eliminar servicios en un servidor Red Hat 5 o 6 (previo systemd) mediante el utilitario chkconfig
.
Deshabilitar y eliminar un servicio
Supongamos que se desea deshabilitar y eliminar el servicio "postgres". Para deshabilitarlo, se debe ejecutar el siguiente comando:
[root@redhat ~]# chkconfig postgres off
Sin embargo, el servicio sigue estando disponible para ser habilitado nuevamente y aparece al listar los servicios con chkconfig
. Para eliminarlo se debe recurrir al comando --del
:
[root@redhat init.d]# chkconfig --del postgres
Esto hace que el servicio no aparezca al listar. Sin embargo chkconfig
aún es capaz de verlo si el script permanece dentro del directorio /etc/init.d/
:
[root@redhat init.d]# chkconfig --list postgres service postgres supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add postgres')
Para eliminarlo definitivamente, luego de ejecutar chkconfig --del
es necesario borrar el script de inicio (o link simbólico al script de inicio):
[root@redhat init.d]# rm postgres rm: remove symbolic link `postgres'? y
Ahora ha desaparecido definitivamente y el gestor de inicio es incapaz de accederlo:
[root@redhat init.d]# chkconfig --list postgres error reading information on service postgres: No such file or directory
Agregar un servicio
Para agregar un nuevo servicio, el script de inicio debe tener el formato LSB. Por ejemplo:
[root@redhat init.d]# cat /db/db1/postgresql-db1 #!/bin/bash ### BEGIN INIT INFO # Provides: postgresql-db1 # Required-Start: $local_fs $network $syslog $named # Required-Stop: $local_fs $network $syslog $named # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: db1 # Description: PostgreSQL db1 ### END INIT INFO . /etc/profile if [ $# -eq 0 ] ; then echo "Se debe indicar el comando: start|stop|stopnow|restart|reload|status" exit 1 fi PGDATA="/db/db1" LOG="${PGDATA}/pg_log/online$(date +%Y%m%d%H%M%S).log" COMANDO=$1 if [ "${COMANDO}" == "stopnow" ] ; then PARAMETROS_EXTRA="--mode=fast" COMANDO="stop" else PARAMETROS_EXTRA="" fi su - postgres -c "/usr/local/pgsql/bin/pg_ctl ${COMANDO} ${PARAMETROS_EXTRA} -D ${PGDATA} -l ${LOG}"
Si el script no se encuentra en el directorio /etc/init.d/
es posible crear un enlace simbólico:
[root@redhat init.d]# ln -s /db/db1/postgresql-db1 .
[root@redhat init.d]# ll postgresql* lrwxrwxrwx 1 root root 42 Aug 18 09:28 postgresql-db1 -> /db/db1/postgresql-db1
Para habilitar el servicio es posible ejecutar chkconfig SERVICIO on
o chkconfig --add SERVICIO
:
[root@redhat init.d]# chkconfig postgresql-db1 on
Luego es posible comprobar que se ha agregado correctamente listando el servicio con chkconfig
:
[root@redhat init.d]# chkconfig --list postgresql-db1 postgresql-db1 0:off 1:off 2:on 3:on 4:on 5:on 6:off
A partir de este momento es posible gestionar el servicio con el utilitario service
:
[root@redhat ~]# service postgresql-db1 status pg_ctl: server is running (PID: 3871) /usr/local/pgsql/bin/postgres "-D" "/db/db1"
Referencias