fish shell 如何获取后台启动进程的PID

Fish shell how to get the PID of the process started in the background

在 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

如何获取后台进程的PID?

使用process expansion,你可以写

set PID %1          # if you know it's the first background job
set PID %something  # if you know it's the only "something" running

请注意,此功能是 removed in fish version 3

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

set PID (jobs -l | awk '{print }')    # just get the pid
jobs -l | read jobid pid cpu state cmd  # get all the things

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