start-stop-daemon 管理后台程序

发布时间: 更新时间: 总字数:870 阅读时间:2m 作者: IP上海 分享 网址

在 debian 和 ubuntu 中,start-stop-daemon 用于启动和停止系统守护程序。

介绍

  • start-stop-daemon 命令来源与 dpkg 包中:
$ dpkg -S start-stop-daemon
dpkg: /usr/share/man/fr/man8/start-stop-daemon.8.gz
dpkg: /usr/share/man/man8/start-stop-daemon.8.gz
dpkg: /usr/share/man/nl/man8/start-stop-daemon.8.gz
dpkg: /usr/sbin/start-stop-daemon
  • man start-stop-daemon 查看详细的帮助信息
  • help 信息
start-stop-daemon --help ...
$ start-stop-daemon --help
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):
      --pid <pid>               pid to check
      --ppid <ppid>             parent pid to check
  -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
      --notify-await            wait for a readiness notification
      --notify-timeout <int>    timeout after <int> seconds of notify wait
  -C, --no-close                do not close any file descriptor
  -O, --output <filename>       send stdout and stderr to <filename>
  -m, --make-pidfile            create the pidfile before starting
      --remove-pidfile          delete the pidfile after stopping
  -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
  • 部分参数说明:
    • -n, --name process-name 检查名称为 process-name 的进程。进程名通常是进程文件名,但也有可能被进程自己更改了,可能已被进程本身更改?
    • --exec <程序> -- <程序参数>

使用

  • 示例 ping.sh 脚本
$ cat /usr/local/bin/ping.sh
#!/bin/bash

ping xiexianbin.cn
$ chmod a+x /usr/local/bin/ping.sh
  • root 用户启动一个名字为 long-ping 的守护进程,pid 文件为 /run/long-ping.pid,启动程序为 /usr/local/bin/ping.sh,启动参数为 --daemon
$ start-stop-daemon --start --oknodo --user root --name long-ping \
	--pidfile /run/long-ping.pid --make-pidfile --startas /usr/local/bin/ping.sh \
	--chuid root --background --output /tmp/ping.log # -- --daemon
  • long-ping 发送 SIGTERM,最多等待 5 秒钟让它停下来
# start-stop-daemon --stop --oknodo --user root --name long-ping \
	--pidfile /run/long-ping.pid --retry 5
  • 停止服务
$ start-stop-daemon --stop --name ping.sh
$ start-stop-daemon --stop --name ping  # 产生孤儿进程,需要再杀一次
$ start-stop-daemon --stop --pidfile /run/long-ping.pid
# start-stop-daemon --stop --oknodo --user root --name long-ping \
	--pidfile /run/long-ping.pid --retry=TERM/30/KILL/5
  • 已知问题
    • start-stop-daemon 是一个用于管理后台守护进程的工具,它在停止服务时可能会产生孤儿进程。这通常是由于父进程(即调用 start-stop-daemon 的进程)在终止时没有正确地终止子进程所导致的
    • 解决这个问题的一种方法是确保父进程在调用 start-stop-daemon 之后能够优雅地终止所有相关的子进程

参考

Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数