busybox start-stop-daemon 使用

        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 $?

linux系统命令start-stop-daemon的源码及二进制,其中也提供了一个服务启动脚本模板。 此程序能帮助你实现将命令行程序变成服务运行,比如将"java -jar xxx.jar" 放在后台执行。 ./start-stop-daemon --help start-stop-daemon 1.9.18 for Debian - small and fast C version written by Marek Michalkiewicz , public domain. Usage: start-stop-daemon -S|--start options ... -- arguments ... start-stop-daemon -K|--stop options ... start-stop-daemon -H|--help start-stop-daemon -V|--version Options (at least one of --exec|--pidfile|--user is required): -x|--exec program to start/check if it is running -p|--pidfile pid file to check -c|--chuid change to this user/group before starting process -w|--chdir change the work directory to 'dir' -u|--user | stop processes owned by this user -n|--name stop processes with this name -s|--signal signal to send (default TERM) -a|--startas program to start (default is ) -N|--nicelevel add incr to the process's nice level -b|--background force the process to detach -m|--make-pidfile create the pidfile before starting -R|--retry 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 is |//... where is one of -|[-] send that signal wait that many seconds forever repeat remainder forever or may be just , meaning //KILL/ Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo) 3 = trouble 2 = with --retry, processes wouldn't die
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值