Bash – How to start process at foreground saving it’s pid to a file

bashprocess

Is there a way to save pid while starting a process? The script should not return to a command line, until started process finishes. And the possibility to end process by Ctrl+C should be kept.

Best Answer

command &
echo $! > file
fg > /dev/null

If there's no job control, first turn on monitor mode with

set -m

More about monitor mode here: Turning off the monitor mode in Bash.

Related Question