이 저장소에는 Linux OS 용 데몬의 간단한 예제가 포함되어 있습니다. 이 저장소에는 시작 스크립트의 예도 포함되어 있습니다.
매우 간단한 데몬을 만들려면 매우 쉽습니다. C에서 이와 같은 글을 쓰고 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 );
}
} simple-daemon.service 라는 매우 간단한 Systemd 서비스 파일을 작성하십시오.
[Unit]
Description=Super simple daemon
[Service]
Type=simple
ExecStart=/usr/bin/daemon
[Install]
WantedBy=multi-user.target
그런 다음 Unix 데몬으로 실행할 수 있지만 이러한 데몬에는 Reloadin 구성 파일, 로그 등과 같은 멋진 기능이 없습니다.이 저장소 및 소스는 Unix Daemons의 작동 방식을 이해하는 데 도움이됩니다.
데몬의 예를 만들려면 다음 도구가 있어야합니다.
데몬의 예를 만들려면 다음 명령을 입력해야합니다.
git clone https://github.com/jirihnidek/daemon.git
cd daemon
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr ../
make
sudo make install
명령 줄에서 실행 데몬을 테스트 할 수 있습니다.
./bin/daemon
그러나 이런 식으로 앱을 실행하는 것은 데몬을 실행하는 것이 아닙니다. 명령 줄 매개 변수 및 인수를 살펴 보자.
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
매개 변수 --daemon 또는 -d 로 ./bin/daemon 실행하면 실제 유닉스 데몬이됩니다. 그러나 이것은 오늘날 유닉스 데몬이 어떻게 시작되는지가 아닙니다. 일부 Init 스크립트 또는 서비스 파일 이이 목적으로 사용됩니다.
SystemD를 사용하여 Linux 배포를 사용하면 사용을 시작할 수 있습니다.
systemctl start simple-daemon
systemctl status simple-daemon
systemctl reload simple-daemon
systemctl stop simple-daemon
참고 : 단위 파일
simple-daemon.service및forking-daemon.servicemake install명령을 사용하여 설치하는 동안 디렉토리/usr/lib/systemd/system에 복사됩니다.
Redhat 4/5/6 또는 Centos를 사용하면 Init 스크립트를 사용할 수 있습니다.
cp daemon.init /etc/rc.d/init.d/daemond
그런 다음 다음을 사용하여 데몬을 제어 할 수 있어야합니다.
service daemon start
service daemon status
service daemon reload
service daemon stop