使用服务启动的 Strace 守护进程

Yog*_*esh 5 system-calls strace systemd

通过为进程传递命令,可以使用 Strace 来跟踪进程,如下所示

strace -f -tt -o strace.log -D <SOME_COMMAND>

但是下面的命令无法跟踪已启动守护进程的系统调用

strace -f -tt -o strace.log -D service nginx start

在这种情况下,strace 只是跟踪系统调用/usr/sbin/service并终止。它不会跟踪nginx由于以下原因启动的进程的系统调用service nginx start

如何跟踪由 启动的进程/usr/sbin/service?专门寻找仅使用守护进程的解决方案!

Tar*_*ani 6

而不是运行nginxfrom 服务。运行service nginx stop然后运行

strace nginx -g "daemon off;"
Run Code Online (Sandbox Code Playgroud)

这将确保您获得该过程的踪迹。这-g "daemon off;"将确保 nginx 不作为守护进程运行,否则 strace 将再次结束

服务命令只是激活一个进程,如果您想要它,strace最好是直接启动该进程。

如果您仍然有兴趣调试使用 service 命令启动的进程。然后执行下面的操作

service nginx start
ps aux | grep nginx
Run Code Online (Sandbox Code Playgroud)

pid从 nginx 进程捕获,然后使用附加到它

strace -p <pid>
Run Code Online (Sandbox Code Playgroud)

分叉进程

要跟踪哪个进程分叉,您需要使用该-f标志

strace -f nginx
Run Code Online (Sandbox Code Playgroud)

服务追踪

当您调用时service start nginx,假设系统使用systemd,调用将被转换为systemctl start nginx. 现在如果你看一下源代码systemd

https://github.com/systemd/systemd/blob/cf45dd36282368d5cdf757cac2cf1fc2b562dab2/src/systemctl/systemctl.c#L3100

r = sd_bus_call_method_async(
    bus,
    NULL,
    "org.freedesktop.systemd1",
    "/org/freedesktop/systemd1",
    "org.freedesktop.systemd1.Manager",
    "Subscribe",
    NULL, NULL,
    NULL);
Run Code Online (Sandbox Code Playgroud)

它不会产生/分叉进程。它将消息发送到 systemd 服务,然后启动nginx进程。

简而言之,,您无法跟踪您的service nginx start命令。