鱼壳如何在后台启动进程的PID

Ale*_*tos 9 fish

在FiSH(友好交互式SHell)中,我可以在后台开始一个过程(something &).但是,如果我尝试检索进程的进程ID(PID=$!),我会收到来自fish的错误:

fish: Unknown command “PID=$!”. Did you mean “set PID $!”? For information on assigning values to variables, see the
help section on the set command by typing “help set”.
PID=$!: command not found
Run Code Online (Sandbox Code Playgroud)

如何检索后台进程的PID?

gle*_*man 8

使用流程扩展,您可以编写

set PID %1          # if you know it's the first background job
set PID %something  # if you know it's the only "something" running
Run Code Online (Sandbox Code Playgroud)

请注意,鱼版本3中删除此功能.

否则,我们可以使用该jobs命令

set PID (jobs -l | awk '{print $2}')    # just get the pid
jobs -l | read jobid pid cpu state cmd  # get all the things
Run Code Online (Sandbox Code Playgroud)

  • 我也使用`set PID(jobs -lp)`而不是'awk`."-p"参数给出了PID (4认同)

Dam*_*ers 6

Fish 版本 3开始,您可以使用$last_pid来替代 来%last获取最后一个后台作业的 PID。