Write a PID file manually from Bash script

Solution 1:

You can easily run the process in the background with "&", then get the PID of the background process using "$!"

#!/bin/bash
long_running_tool &
echo $! > pid_file

Then, optionally, wait $! if you want the shell to block until the process completes execution.

Solution 2:

#!/bin/bash
echo $$ > fooapp.pid
exec fooapp

As mentioned earlier, exec replaces bash with the specified program, retaining the PID.