How to get the PID of a process to start in the background

During FiSH (Friendly Interactive Shell), I can start the process in the background ( something &). However, if I try to get the process ID of the process ( PID=$!), I get an error message from the 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

How can I get the PID of the background process?

+4
source share
1 answer

Using the process extension , you can write

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

Otherwise, we can use the command jobs

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

Source: https://habr.com/ru/post/1589258/


All Articles