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}')"

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.

1 Answer 1


You can't overwrite internal PID value. Use lowercase environment variables.

  • I didn't realise I was doing that, thank you!
    – Thermatix
    Oct 24, 2017 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.