【问题标题】:Stopping android logcat that is executing in a child process停止在子进程中执行的 android logcat
【发布时间】:2023-05-25 18:59:01
【问题描述】:

我正在编写一个 ruby​​ 脚本,我想在其中执行 android logcat -v time 命令在子进程中并在一段时间后终止该进程(当父进程完成 xyz 时)。

例如我有: childpid = Process.fork { adb -s <device-serial> logcat -c adb -s <device-serial> logcat -v time>/path-to-file/forkLog.log }

sleep(30)
    #parent do thing else here ...
Process.kill("SIGHUP", childpid)  #kill the child process

根据我的阅读,adb logcat 代码是在另一个子进程中执行的,所以当我尝试执行 Process.kill 时,childpid 会停止,但它的子进程不会。

如何从父进程中杀死 logcat -v time>forklog.log 进程??

【问题讨论】:

    标签: android ruby process android-logcat kill-process


    【解决方案1】:

    通常,当您使用 fork 调用 bash 脚本时,您需要使用 Kernel::exec() 而不是 Kernel::system() 或 Kernel::`

    Process.fork{exec('adb -s <device-serial> logcat -c && adb -s  <device-serial> logcat -v time>/path-to-file/forkLog.log'}
    

    不同之处在于 exec 会将 Ruby 进程替换为 shell 脚本,而另外两个将创建一个子进程。

    【讨论】:

    • 谢谢。我试过了,但它似乎不起作用 adb 可能在另一个子进程中运行---在我做了一些进一步的阅读之后
    • 基本上我需要一种方法来杀死所有(伟大的)孙子,然后杀死孩子才能使其工作......但是如何做到这一点?