$1 &
echo $!
是否有不同的方式在后台启动命令并立即返回 pid?
所以当我启动 bash run.sh "python worker.py"
时,它会给我启 Action 业的 pid。
我正在使用 paramiko,这是一个不能与 python worker.py &
一起使用的 python 库。所以我想创建一个 bash 脚本,它将在远程服务器上为我执行此操作。
最佳答案
由于您使用的是 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.
所以在你的情况下,像
工作-l | grep 'worker.py' | awk '{print $2}'
可能会给你你想要的。
关于python - Bash:运行后台作业并获取 pid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23684022/