Este repositorio contiene un ejemplo simple de demonio para el sistema operativo Linux. Este repositorio también contiene ejemplos de scripts iniciales.
Cuando quieras crear un demonio súper simple, entonces es muy fácil. Puedes escribir algo como esto en C y llamarlo daemon.c :
/* Compile this with gcc -o daemon daemon.c */
#include <unistd.h>
int main ( void )
{
while ( 1 ) {
/* TODO: do something usefull here ;-) */
sleep ( 1 );
}
} y escriba un archivo de servicio Systemd súper simple llamado simple-daemon.service :
[Unit]
Description=Super simple daemon
[Service]
Type=simple
ExecStart=/usr/bin/daemon
[Install]
WantedBy=multi-user.target
Y luego puede ejecutarlo como Daemon Unix, pero dicho demonio no tiene algunas características agradables como Reloadin Configurar archivos, registro, etc. Este repositorio y fuentes pueden ayudarlo a comprender cómo funciona Unix Daemons.
Para crear un ejemplo del demonio, debe tener las siguientes herramientas
Para crear un ejemplo de demonio, debe escribir los siguientes comandos:
git clone https://github.com/jirihnidek/daemon.git
cd daemon
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr ../
make
sudo make install
Puede probar la ejecución de Daemon desde la línea de comando:
./bin/daemon
Pero ejecutar la aplicación de esta manera no está ejecutando Daemon. Echen un vistazo a los parámetros y argumentos de la línea de comandos
Usage: ./bin/daemon [OPTIONS]
Options:
-h --help Print this help
-c --conf_file filename Read configuration from the file
-t --test_conf filename Test configuration file
-l --log_file filename Write logs to the file
-d --daemon Daemonize this application
-p --pid_file filename PID file used by daemonized app
Cuando ejecute ./bin/daemon con el parámetro --daemon o -d , se convertirá en un verdadero demonio unix. Pero esta no es la forma en que se inician los demonios Unix hoy en día. Algunos scripts de inicio o archivos de servicio se utilizan para este propósito.
Cuando usa la distribución de Linux usando Systemd, puede probar Start Daemon usando
systemctl start simple-daemon
systemctl status simple-daemon
systemctl reload simple-daemon
systemctl stop simple-daemon
Nota: La unidad archiva
simple-daemon.serviceyforking-daemon.servicese copian en el directorio/usr/lib/systemd/systemdurante la instalación utilizandomake install.
Cuando usa Redhat 4/5/6 o CentOS, puede intentar usar el script init:
cp daemon.init /etc/rc.d/init.d/daemond
Entonces debería ser posible controlar el demonio usando:
service daemon start
service daemon status
service daemon reload
service daemon stop