daemon
daemon-0.2
该存储库包含用于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 Configure Files,Roging等。此存储库和来源可以帮助您了解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时,它将成为真正的Unix守护程序。但这不是现在的方式,现在如何开始。某些初始脚本或服务文件用于此目的。
使用SystemD使用Linux发行版时,您可以尝试使用
systemctl start simple-daemon
systemctl status simple-daemon
systemctl reload simple-daemon
systemctl stop simple-daemon
注意:使用
make installcommand在安装期间,将单元文件simple-daemon.service和forking-daemon.service复制到目录/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