How to run nohup and write its pid file in a single bash statement
This should work:
nohup ./myprogram.sh > /dev/null 2>&1 &
echo $! > run.pid
You already have one ampersand after the redirect which puts your script in background. Therefore you only need to type the desired command after that ampersand, not prefixed by anything else:
nohup ./myprogram.sh > /dev/null 2>&1 & echo $! > run.pid