I'm trying to capture the PID of a process started in a sub-shell so I can check if the process started correctly, the process in question is a puma app.
sudo su - $APPUSER <<-BASH
PID="\$(bundle exec puma -C config/puma.rb)"
"\$(bundle exec puma -C config/puma.rb & export PID=$1)"
"\$(bundle exec puma -C config/puma.rb &)"
PID="\$(ps aux | grep \"[p]uma.*[u]nix.*$APPNAME\" | awk '{ print \$2}')"
BASH
and it returns nothing, weirdly, if I do any of those without a surrounding ""
I get this error:
-bash: line 22: [26740]: command not found
the number in the []
is the pid, I know this because I get this when I run the script again (I check if the pid exists):
app is running with pid: 26740
I think this is happening because I'm doing the commands in a heredoc and passing them to sudo su
How do I get the pid from the background process I started in a sub-shell.