【问题标题】:get process ids for multiple processes returns the same process id in bash获取多个进程的进程 ID 在 bash 中返回相同的进程 ID
【发布时间】:2014-11-02 02:47:19
【问题描述】:

我正在尝试获取多个 bash 进程的进程 ID:

./test_run.sh 你好 回声$$ ./test_run.sh 你好 回声$$ ./test_run.sh hello22 回声$$

test_run.sh 的定义

回声“运行$ 1”

但它们都返回相同的进程 ID,为什么?

【问题讨论】:

    标签: bash


    【解决方案1】:

    $$ 返回当前 shell 进程的 PID。要获取刚刚启动的后台进程的 PID,请使用$!

    ./test_run.sh hello &
    echo $!
    ./test_run.sh hello &
    echo $!
    

    【讨论】:

    • 您所做的只是将参数更改为test_run.sh。这对这个问题有什么影响?
    • $$ 返回相同的 pid 和 $!在更新的问题中返回 ""。我希望 $$ 返回不同的 pids
    • 哦,我看到您不再将它们放在后台$! 仅用于后台进程。为什么您期望$$ 返回不同的 PID?运行命令时,原始 shell 的 PID 不会改变。该命令在子进程中运行,但原始 shell 进程保持不变。
    猜你喜欢
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 2012-02-25
    • 2013-02-28
    • 1970-01-01
    相关资源
    最近更新 更多