【问题标题】:Bash: running background job and getting pidBash:运行后台作业并获取 pid
【发布时间】:2014-05-15 16:35:30
【问题描述】:
$1 &
echo $!

有没有其他方法可以在后台启动命令并立即返回 pid?

所以当我启动 bash run.sh "python worker.py" 时,它会给我启动作业的 pid。

我正在使用 paramiko,这是一个不适用于 python worker.py & 的 Python 库。所以我想创建一个 bash 脚本,它将在远程服务器上为我执行此操作。

【问题讨论】:

  • 是否有不同的方式在后台启动命令并立即返回 pid?
  • 当你执行“bash run.sh”时,你并没有在后台做任何事情——你是在前台做的。
  • 你提到的方式是标准方式。

标签: python linux bash pid paramiko


【解决方案1】:

由于您使用的是 bash,您只需从 jobs 获取后台进程列表,并通过 -l 标志指示它返回 PID。引用man bash

   jobs [-lnprs] [ jobspec ... ]
   jobs -x command [ args ... ]
          The first form lists the active jobs.  The options have the
          following meanings:
          -l     List process IDs in addition to the normal information.

所以在你的情况下,像 jobs -l | grep 'worker.py' | awk '{print $2}' 可能会给你你想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多