• ubuntu12.04 Daemon的简单实现


    使用命令 start-stop-daemon

    官方文档:http://manpages.ubuntu.com/manpages/lucid/en/man8/start-stop-daemon.8.html

    Usage: start-stop-daemon [<option> ...] <command>
    
    Commands:
      -S|--start -- <argument> ...  start a program and pass <arguments> to it
      -K|--stop                     stop a program
      -T|--status                   get the program status
      -H|--help                     print help information
      -V|--version                  print version
    
    Matching options (at least one is required):
      -p|--pidfile <pid-file>       pid file to check
      -x|--exec <executable>        program to start/check if it is running
      -n|--name <process-name>      process name to check
      -u|--user <username|uid>      process owner to check
    
    Options:
      -g|--group <group|gid>        run process as this group
      -c|--chuid <name|uid[:group|gid]>
                                    change to this user/group before starting
                                      process
      -s|--signal <signal>          signal to send (default TERM)
      -a|--startas <pathname>       program to start (default is <executable>)
      -r|--chroot <directory>       chroot to <directory> before starting
      -d|--chdir <directory>        change to <directory> (default is /)
      -N|--nicelevel <incr>         add incr to the process' nice level
      -P|--procsched <policy[:prio]>
                                    use <policy> with <prio> for the kernel
                                      process scheduler (default prio is 0)
      -I|--iosched <class[:prio]>   use <class> with <prio> to set the IO
                                      scheduler (default prio is 4)
      -k|--umask <mask>             change the umask to <mask> before starting
      -b|--background               force the process to detach
      -m|--make-pidfile             create the pidfile before starting
      -R|--retry <schedule>         check whether processes die, and retry
      -t|--test                     test mode, don't do anything
      -o|--oknodo                   exit status 0 (not 1) if nothing done
      -q|--quiet                    be more quiet
      -v|--verbose                  be more verbose
    
    Retry <schedule> is <item>|/<item>/... where <item> is one of
     -<signal-num>|[-]<signal-name>  send that signal
     <timeout>                       wait that many seconds
     forever                         repeat remainder forever
    or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>
    
    The process scheduler <policy> can be one of:
      other, fifo or rr
    
    The IO scheduler <class> can be one of:
      real-time, best-effort or idle
    
    Exit status:
      0 = done
      1 = nothing done (=> 0 if --oknodo)
      2 = with --retry, processes would not die
      3 = trouble
    Exit status with --status:
      0 = program is running
      1 = program is not running and the pid file exists
      3 = program is not running
      4 = unable to determine status
    

      

    demo:

    odoo的启动脚本

    #!/bin/sh
    
    ### BEGIN INIT INFO
    # Provides:             openerp-server
    # Required-Start:       $remote_fs $syslog
    # Required-Stop:        $remote_fs $syslog
    # Should-Start:         $network
    # Should-Stop:          $network
    # Default-Start:        2 3 4 5
    # Default-Stop:         0 1 6
    # Short-Description:    Enterprise Resource Management software
    # Description:          Open ERP is a complete ERP and CRM software.
    ### END INIT INFO
    
    PATH=/bin:/sbin:/usr/bin
    DAEMON=/opt/openerp/server/openerp-server
    NAME=openerp-server
    DESC=openerp-server
    
    # Specify the user name (Default: openerp).
    USER=openerp
    
    # Specify an alternate config file (Default: /etc/openerp-server.conf).
    CONFIGFILE="/etc/openerp-server.conf"
    
    # pidfile
    PIDFILE=/var/run/$NAME.pid
    
    # Additional options that are passed to the Daemon.
    DAEMON_OPTS="-c $CONFIGFILE"
    
    [ -x $DAEMON ] || exit 0
    [ -f $CONFIGFILE ] || exit 0
    
    checkpid() {
        [ -f $PIDFILE ] || return 1
        pid=`cat $PIDFILE`
        [ -d /proc/$pid ] && return 0
        return 1
    }
    
    case "${1}" in
            start)
                    echo -n "Starting ${DESC}: "
    
                    start-stop-daemon --start --quiet --pidfile ${PIDFILE} 
                            --chuid ${USER} --background --make-pidfile 
                            --exec ${DAEMON} -- ${DAEMON_OPTS}
    
                    echo "${NAME}."
                    ;;
    
            stop)
                    echo -n "Stopping ${DESC}: "
    
                    start-stop-daemon --stop --quiet --pidfile ${PIDFILE} 
                            --oknodo
    
                    echo "${NAME}."
                    ;;
    
            restart|force-reload)
                    echo -n "Restarting ${DESC}: "
    
                    start-stop-daemon --stop --quiet --pidfile ${PIDFILE} 
                            --oknodo
    
                    sleep 1
    
                    start-stop-daemon --start --quiet --pidfile ${PIDFILE} 
                            --chuid ${USER} --background --make-pidfile 
                            --exec ${DAEMON} -- ${DAEMON_OPTS}
    
                    echo "${NAME}."
                    ;;
    
            *)
                    N=/etc/init.d/${NAME}
                    echo "Usage: ${NAME} {start|stop|restart|force-reload}" >&2
                    exit 1
                    ;;
    esac
    
    exit 0
    

      

  • 相关阅读:
    NOIP赛前集训备忘录(含每日总结)(日更?。。。)
    饮一碗鸡汤,换我前进的力量(持续更新......)
    各种用来学习的东西总结
    [FJOI2007]轮状病毒 题解(dp(找规律)+高精度)
    洛谷P1823 [COI2007] Patrik 音乐会的等待(单调栈+二分查找)
    [CQOI2012]模拟工厂 题解(搜索+贪心)
    [CQOI2014]数三角形 题解(组合数学+容斥)
    洛谷P2507 [SCOI2008]配对 题解(dp+贪心)
    洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心)
    洛谷P1155 双栈排序题解(图论模型转换+二分图染色+栈)
  • 原文地址:https://www.cnblogs.com/Tommy-Yu/p/4106062.html
Copyright © 2020-2023  润新知