2013-08-30 71 views
2

在一个shell程序中,我想启动一个程序并获取其PID并保存在临时文件中。但在这里,我会在前台启动该程序,直到进程处于运行状态在前台启动进程时获取Shell脚本中的进程ID

前不会退出shell:

而且这工作得很好,我能够得到启动的PID过程。但是,如果我在发动地面脱颖而出的节目,我想知道如何获得PID

例如:

#!/bin/bash 

myprogram  /// hear some how i wan to know the PID before going to next line 
+4

如果您的程序在前台运行,那么您将如何进入下一行? – anubhava

+0

或者是否有可能在后台启动程序并获取进程ID,然后等待脚本直到该进程退出? –

+0

是的,可以完成,请参阅下面我编辑的答案。 – anubhava

回答

0

你不能这样做,因为你的shell脚本没有运行 - 您的命令刚刚在前台推出是。

8

正如我上面评论,因为你的命令仍然在前台运行,你不能在同一个shell中输入一个新的命令并转到下一行。

然而当这个命令正在运行,你想从不同的外壳标签/窗口过程获得此程序的进程ID,然后使用pgrep这样的:

pgrep -f "myprogram" 
17113 # this # will be different for you :P 

编辑:基地您的评论or is it possible to launch the program in background and get the process ID and then wait the script till that process gets exited ?

是可以使用wait pid命令如下进行:

myprogram & 
mypid=$! 
# do some other stuff and then 
wait $mypid 
+0

@NaggappanRM:这是否适合你? – anubhava