zoukankan      html  css  js  c++  java
  • linux守护进程start-stop-daemon启动服务

    start-stop-daemon

    #! /bin/sh
    
    PATH=/sbin:/bin
    
    . /lib/lsb/init-functions
    
    do_start () {
       log_action_msg "do_start tftp here"
    
       if start-stop-daemon --start --background --pidfile /var/run/tftp.pid 
       --exec /home/zxc/tftp/tftps; then               
          log_action_msg "do_start succ"
          log_end_msg 0 || true
       else
          log_action_msg "do_start error"
          log_end_msg 1 || true
       fi
    }
    
    do_stop () {
       log_action_msg "do_stop tftp here"
    
       if start-stop-daemon start-stop-daemon --stop --name tftps; then
          log_action_msg "do_stop succ"
          log_end_msg 0 || true
       else
          log_action_msg "do_stop error"
          log_end_msg 1 || true
       fi
    }
    
    case "$1" in
       start)
          do_start
          ;;
       restart|reload|force-reload)
          echo "Error: argument '$1' not supported" >&2
          exit 3
          ;;
       stop)
          do_stop
          ;;
       status)
          status_of_proc -p /var/run/tftp.pid /home/zxc/tftp/tftps tftps 
          && exit 0 || exit $?
          ;;
       *)
          echo "Usage: $0 start|stop" >&2
          exit 3
          ;;
    esac
    

    /var/run/*.pid

    #define PID_FILE "/var/run/tftp.pid"
    
    int lock_file(int fd)
    {
        struct flock fl;
        fl.l_type = F_WRLCK;
        fl.l_start = 0;
        fl.l_whence = SEEK_SET;
        fl.l_len = 0;
    
        return (fcntl(fd, F_SETLK, &fl));
    }
    
    int alone_runnind(void)
    {
        int fd;
        char buf[16];
    
        fd = open(PID_FILE, O_RDWR | O_CREAT, 0666);
        if (fd < 0)
        {
            perror("open");
            exit(1);
        }
    
        if (lock_file(fd) < 0)
        {
            if (errno == EAGAIN)
            {
                close(fd);
                printf("alone runnind
    ");
                return -1;
            }
    
            printf("can't lock %s: %s
    ", PID_FILE, strerror(errno));
        }
    
        ftruncate(fd, 0);
        sprintf(buf, "%ld
    ", (long)getpid());
        write(fd, buf, strlen(buf));
    
        close(fd);
    
        return 0;
    }
    
  • 相关阅读:
    java要注意的问题1
    广义表(线性表的推广)
    java基本类型和包装器类
    面试题10:斐波那契数列
    面试题9:用两个栈实现队列
    面试题8:二叉树的下一个结点
    10 分组数据
    9 汇总数据
    8 使用数据处理函数
    7 创建计算字段
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709290.html
Copyright © 2011-2022 走看看