start-stop-daemon 用来创建和停止系统的进程,在嵌入式linux系统经常用来拉起一些系统服务程序,比如嵌入式系统的主程序和一些依赖的服务。
1、测试程序
我们编写如下的一个demo,模拟系统的启动服务。
/*================================================================
* Copyright (C) 2025 Sangfor Ltd. All rights reserved.
*
* 文件名称:test.c
* 创 建 者:HeXXXX
* 创建日期:2025年xx月xx日
* 描 述:
*
================================================================*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
int i = 0;
for(i = 1; i < argc; i++){
printf("arg[%d] = %s\n",i - 1, argv[i]);
}
while(1){
sleep(1);
printf("hello world!\n");
}
return 0;
}
2、命令帮助输出
~ # start-stop-daemon
BusyBox v1.20.2 (2018-10-25 09:38:30 CST) multi-call binary.
Usage: start-stop-daemon [OPTIONS] [-S|-K] ... [-- ARGS...]
Search for matching processes, and then
-K: stop all matching processes.
-S: start a process unless a matching process is found.
Process matching:
-u,--user USERNAME|UID Match only this user's processes
-n,--name NAME Match processes with NAME
in comm field in /proc/PID/stat
-x,--exec EXECUTABLE Match processes with this command
in /proc/PID/{exe,cmdline}
-p,--pidfile FILE Match a process with PID from the file
All specified conditions must match
-S only:
-x,--exec EXECUTABLE Program to run
-a,--startas NAME Zeroth argument
-b,--background Background
-N,--nicelevel N Change nice level
-c,--chuid USER[:[GRP]] Change to user/group
-m,--make-pidfile Write PID to the pidfile specified by -p
-K only:
-s,--signal SIG Signal to send
-t,--test Match only, exit with 0 if a process is found
Other:
-o,--oknodo Exit with status 0 if nothing is done
-v,--verbose Verbose
-q,--quiet Quiet
3、常用命令解析
start-stop-daemon 的功能非常丰富,我们只解释一下常用的命令。
-S, --start [--] arguments
检查后台是否存在指定的进程。如果存在这样的进程,start-stop守护进程将不执行任何操作,并以错误状态1退出(如果指定了--oknodo,则为0)。如果这样的进程不存在,它将使用--exec指定的可执行文件或--startas(如果指定了)启动一个实例。命令行上 -- 之后给出的任何参数都会原封不动地传递给正在启动的程序。
[tmp]$start-stop-daemon --start --exec /tmp/test -- -1 -2 -3
arg[0] = -1
arg[1] = -2
arg[2] = -3
hello world!
hello world!
hello world!
-K, --stop
检查是否存在指定的进程。如果存在这样的进程,start-stop守护进程会向其发送--signal指定的信号,并以错误状态0退出。如果不存在这样的进程,启动-停止守护进程将退出,错误状态为1(如果指定了--oknodo,则为0)。如果指定了-retry,则start-stop守护进程将检查进程是否已终止。
#执行后,前台运行的程序马上退出
start-stop-daemon --stop --exec /tmp/test
-p, --pidfile pid-file
检查进程是否已创建文件pid文件。注意:如果旧进程在无法删除pid文件的情况下终止,可能会出现异常情况
$start-stop-daemon --start --pidfile /var/run/test.pid --background --exec /tmp/test -- -1 -2 -3
$start-stop-daemon --stop --pidfile /var/run/test.pid
-n, --name process-name
检查是否存在名为进程名的进程。进程名称通常是进程文件名。
-S only: -S或者 --start后仅仅支持的参数
-x, --exec executable
检查作为此可执行文件实例的进程。可执行参数应该是绝对路径名。如果可执行程序后面有参数的话。需要使用"--"字符串分离
[tmp]$start-stop-daemon --start --exec /tmp/test -- -1 -2 -3
arg[0] = -1
arg[1] = -2
arg[2] = -3
hello world!
hello world!
hello world!
-b, --background
将实例进程后台执行。
其他:
-q, --quiet
不要打印信息性消息;仅显示错误消息。
$start-stop-daemon --start --quiet --exec /tmp/test -- -1 -2 -3
start-stop-daemon --stop --quiet --exec /tmp/test
-o, --oknodo
当使用start-stop-daemon关闭和打开系统进程的时候。如果后台程序已经默认打开,这时在进行打开动作,会返回1.调用这个参数是会返回0, 关闭动作类似。
4、测试脚本
#! /bin/sh
### BEGIN INIT INFO
# Provides: test.sh
# Required-Start:
# Required-Stop:
# Default-Start: S
# Default-Stop: 2 3 4 5
# Short-Description: One of the first scripts to be executed. Starts or stops
#
### END INIT INFO
DAEMON=/tmp/test
ACTION="$1"
start(){
echo -n "Starting test: "
start-stop-daemon --start --quiet --oknodo --background --exec $DAEMON -- -1 -2 -3
echo "OK"
}
stop(){
echo -n "Stopping test: "
start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
echo "OK"
}
restart(){
stop
start
}
case "$ACTION" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage $0 $1 {start/stop/restart}"
exit 1
esac
exit $?