守护进程daemon的创建和使用

                             daemon的创建和使用  


创建守护进的关键步骤:

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

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

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

step 4.重设文件权限掩码

step 5.关闭文件描述符

以下是一个简单的daemon例子

代码:

附1

附2

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

221202_jcm1_556678.png

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

221227_EhZ6_556678.png

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

然后执行 chkconfig  - -add daemon-script 

221249_LunU_556678.png

可以看到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
 

转载于:https://my.oschina.net/u/556678/blog/183780

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值