I am trying to get the process ID of the last executed command from within a bash script. Normally I would do:
$ nohup ./test > nhout &
nohup: redirecting stderr to stdout
$ echo $!
3006196 #This is the PID
I am trying to do the exact same thing from within a shell script.
#!/bin/bash
for i in {1..10};do
`nohup ./testscript_${i} > nhout_${i} &`
echo $! >> pid_list.txt
done
However, when I check pid_list.txt, the file is empty. I understand this could be because $! refers to the script that I ran rather than the nohup command. But I don't know how to fix this and couldn't find any resources online either. How would I achieve this?