【问题标题】:$! is not working for PID of last forked process美元!不适用于最后一个分叉进程的 PID
【发布时间】:2017-06-28 11:19:28
【问题描述】:

我正在尝试使用 --fork 在后台启动一个进程,它会启动,但我也想使用 PID 终止该进程。为此,我想将分叉进程的 PID 存储在 .pid 文件中使用

echo $!> pidfile

但这不起作用,只是给出一个空字符串。我还附上了代码 sn-p。请帮忙。 提前致谢!

#!/bin/bash
start() {
    mongod --shardsvr --dbpath /user/JCBLivelink/MongoDB/shardA --port 27020 --logpath /user/JCBLivelink/MongoDB/shardA.log --logappend --fork
    echo $!>/user/JCBLivelink/MongoDB/shardA.pid
}

【问题讨论】:

  • 您可以使用pgrep monod 来获取进程的pid。这不是一个简洁的解决方案,如果有多个实例也不正确,但它是一种可能对您有用的临时解决方法。
  • 守护进程通常分叉两次。

标签: linux bash shell background-process pid


【解决方案1】:

$! 仅在 shell 是创建后台进程时才有效。在这种情况下,mongod 正在执行此操作,而 shell 对此一无所知。

也就是说,您可以指定 --pidfilepathmongod 让它为您执行此操作

【讨论】:

    猜你喜欢
    • 2012-09-15
    • 2016-04-03
    • 1970-01-01
    • 2018-07-02
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 2017-11-29
    相关资源
    最近更新 更多