Bash:运行后台工作并获得pid

时间:2014-05-15 16:35:30

标签: python linux bash pid paramiko

$1 &
echo $!

是否有不同的方法在后台启动命令并立即返回pid?

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

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

1 个答案:

答案 0 :(得分: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}'可能会给你你想要的东西。

相关问题