• start-stop-daemon 守护进程管理


    start-stop-daemon 守护进程管理

    start-stop-daemon 作为系统自带,简单实用 结合systemctl,用起来很是不错.

    示例

    PIDFILE=/var/run/nginx.pid
    DAEMON=/usr/local/nginx
    DAEMON_OPTS="-c /tmp/nginx/nginx.conf"
    start-stop-daemon --start --quiet --make-pidfile  --pidfile $PIDFILE --exec $DAEMON/sbin/nginx -- $DAEMON_OPTS
    

    上面执行指令通过 start-stop-daemon 来启动一个nginx进程,并且生成一个pid文件。

    注意这里只是一个实例,用来说明 start-stop-daemon 的运作方式,真实环境中Nginx不需要这样启动。

    参数介绍

    • --start 启动一个守护进程
    • --stop 终止一个守护进程
    • --status 查看一个守护进程运行状态
    • --pidfile 记录进程号(pid)的文件
    • --exec 启动进程的入口
    • --user 启动进程的用户
    • --make-pidfile 如果进程自己不创建pidfile,可以通过该参数指定
    • --quiet 不输出警告信息
    • --retry 启动失败后重试的次数
    • --background 指定为后台运行模式,如果进程不是后台模式则需加如这个参数
    • -- 空格之后的参数是传给--exec 的 如上列也就是/usr/local/nginx/sbin/nginx

    --status 的几个状态说明

       0      Program is running.
       1      Program is not running and the pid file exists.
       3      Program is not running.
       4      Unable to determine program status.
    

    一个完整的例子

    cat /etc/init.d/frpc

    #!/bin/sh
    
    ### BEGIN INIT INFO
    # Provides:          frpc
    # Required-Start:    $network $local_fs $remote_fs
    # Required-Stop:     $network $local_fs $remote_fs
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Should-Start:      slapd cups
    # Should-Stop:       slapd cups
    # Short-Description: Frpc keep service
    # Description: server to provide Frpc
    ### END INIT INFO
    
    
    PIDDIR=/var/run/frp
    FRPCPID=$PIDDIR/frpc.pid
    
    # clear conflicting settings from the environment
    unset TMPDIR
    
    # See if the daemons are there
    test -x /home/makeit/apps/frp_0.27.0_linux_amd64/frpc || exit 0
    
    . /lib/lsb/init-functions
    
    case $1 in
    	start)
    
    		log_daemon_msg "Starting frpc daemon" frpc
    		# Make sure we have our PIDDIR, even if it's on a tmpfs
    		install -o root -g root -m 755 -d $PIDDIR
    
    		if ! start-stop-daemon --start  --background --retry 30 --make-pidfile   --pidfile $FRPCPID --exec /home/makeit/apps/frp_0.27.0_linux_amd64/frpc -- -c /etc/frpc.ini ; then
    			log_end_msg 1
    			exit 1
    		fi
    
    		log_end_msg 0
    		;;
    	stop)
    
    		log_daemon_msg "Stopping FRPC daemon" frpc
    
    		start-stop-daemon --stop  --pidfile $FRPCPID
    		#kill `cat $FRPCPID`
    		# Wait a little and remove stale PID file
    		sleep 1
    		if [ -f $FRPCPID ] && ! ps h `cat $FRPCPID` > /dev/null
    		then
    			# Stale PID file, remove it (should be removed by
    			# smbd itself IMHO).
    			rm -f $FRPCPID
    		fi
    
    		log_end_msg 0
    
    		;;
    	reload)
    		log_daemon_msg "Reloading /etc/frpc.ini" frpc
    		$0 restart
    		;;
    	restart|force-reload)
    		$0 stop
    		sleep 1
    		$0 start
    		;;
            status)
    		status_of_proc -p $FRPCPID /home/makeit/apps/frp_0.27.0_linux_amd64/frpc frpc
    		exit $?
    		;;
    	*)
    		echo "Usage: /etc/init.d/frpc {start|stop|reload|restart|force-reload|status}"
    		exit 1
    		;;
    esac
    
    exit 0
    
    

    注意修改完 要执行 systemctl daemon-reload 重载修改的文件

  • 相关阅读:
    lines-HDU5124(区间处理 +离散化)
    Reorder the Books-HDU5500
    Bad Hair Day-POJ3250(简单的入栈出栈)
    Count the Colors-ZOJ1610(线段树区间求)
    Just a Hook-HDU1698(线段树求区间)
    Mayor's posters-POJ2528(线段树+离散化)
    A Simple Problem with Integers-POJ3468
    Strongly connected-HDU4635
    Caocao's Bridges-HDU4738(Tarjin+求桥)
    Warm up-HUD4612(树的直径+Tarjin缩点)
  • 原文地址:https://www.cnblogs.com/lovesKey/p/11335085.html
Copyright © 2020-2023  润新知