您的位置:首页 > 运维架构 > Linux

守护进程daemon的创建和使用

2013-12-11 00:00 429 查看
摘要: 《Unix高级环境编程》中介绍了怎么写一个daemon的基本步骤,但是没有讲到在怎么在系统中使用(即像apache那样开机启动,关机停止),在这里写了一个简单的例子供大家参考。

daemon的创建和使用

创建守护进的关键步骤:

step 1.创建子进程,父进程退出

step 2.在子进程中创建新会话

step 3.改变当前目录为根目录

step 4.重设文件权限掩码

step 5.关闭文件描述符

以下是一个简单的daemon例子

代码:

附1

附2

将file.c编译成名为 simple-daemon



然后在脚本daemon-script中编辑程序的名字和路径



将脚本daemon-script 复制到/etc/init.d/目录下

然后执行 chkconfig - -add daemon-script



可以看到chkconfig默认在run level 2 3 4 5下通过deamon-script脚本来启动指定的程序simple-daemon,在run level 0 1 6下通过deamon-script脚本来关闭指定的程序simple-daemon。

也可以在terminal中以管理员的身份执行

service daemon-script start //启动,当然系统默认是开启的

service daemon-script stop //停止

service daemon-script restart //重启

附1:

//this app write the system time to testFile every two seconds
//这个程序将会每间隔2秒在文件中写入系统时间(date),你可以在下面 arr 中定义文件的位置
//  file.c
//
//  Created by Jialin Wu on 11/12/13.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
void daemonize(void) {
pid_t  pid;

/*
* Become a session leader to lose controlling TTY.
*创建子进程,父进程退出
*/
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
} else if (pid != 0) /* parent */
exit(0);

//在子进程中创建新会话
setsid();

/*改变当前目录为/目录
*如果程序要与数据库有关,可能会在更改当前目录后出不现莫名奇妙的错误,可尝试不更改当前目录
* Change the current working directory to the root.
* if you are using database  just ignore this and comment them.
*/
/*if (chdir("/") < 0) {
perror("chdir");
exit(1);
}
*/

//重设文件权限掩码
umask(0);

/*关闭文件描述符
*
*/
close(0);
close(1);
close(2);

//Attach file descriptors 0, 1, and 2 to /dev/null.可选
open("/dev/null", O_RDWR);
dup2(0, 1);
dup2(0, 2);

}

int main(int argc, const char *argv[])
{
int file;
struct tm* newtime;
time_t ttime;

daemonize();    //这个functione的作用就是使之成为daemon

while(1){
ttime=time(NULL);
newtime = localtime(&ttime);

char* arr= "/Users/jialin/Desktop/testFile"; //you might change the file path you want
file = open(arr,O_WRONLY|O_CREAT);
system("date >> /Users/jialin/Desktop/testFile");
sleep(2);
close(file);
}

return 0;
}

附2:

#!/bin/bash
### BEGIN INIT INFO
#
# Default-Start:  2 3 4 5
# Default-Stop:  0 1 6
# Description:  This file should be used to construct scripts to be placed in /etc/init.d.
#
# to use make this script work, for example:
# if you are using ubuntu:
# update-rc.d scriptName defaults 20 80
# for more information find update-rc.d

# if you are using centOS or redHat:
#   chkconfig --add scriptName

#   defaults are 2345 on and 016 off so you can ignore the following steps if not try the followings:
#   chkconfig --level 2345 scriptName on
#   chkconfig --level 016 scriptNmae off

#   for more information find chkconfig

### END INIT INFO

###########################
#start writing the script#
###########################
# Source function library  choose a library according to your linux distribution    and now I'm using centOS
#选择function library ,不同的Linux发行版function library 的位置不不相同
##ubuntu##
#. /lib/lsb/init-functions  如果在是ubuntu下的,把这句去掉注释,把下面centOS 加注释
##ubuntu##

##centOS or redHat##  我现在用的是centOS
. /etc/rc.d/init.d/functions
##centOS or redHat##

## Fill in name of program here.
PROG="guetoj_scheduler"
PROG_PATH="/home/jialin/judger" ## Not need, but sometimes helpful (if $PROG resides in /opt for example).

start() {
$PROG_PATH/$PROG
echo "$PROG started"
}

stop() {
echo "begin stop"
killall $PROG
echo "$PROG stopped"
}

# Check to see if we are running as root first.
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi

case "$1" in
start)
start
exit 0
;;
stop)
stop
exit 0
;;
reload|restart|force-reload)
stop
start
exit 0
;;
**)
echo "Usage: $0 {start|stop|reload}" 1>&2
exit 1
;;
esac
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息