This answer explains how to get the pid of the new process when using Perl's exec(). The pid doesn't even change, so all you need to do is get the pid of the original script. But it doesn't work if I redirect the output to a file as part of the command, which I need to do.
say "my pid is $$";
exec("childscript.pl"); # same pid
But if I redirect the output as part of the command:
say "my pid is $$";
exec("childscript.pl > log.txt"); # different pid, usually old pid + 1
exec("childscript.pl > log.txt 2>&1 &"); # same
then the new pid is one higher than the old one (which is probably just because they were spawned in succession and not reliable). I tested this both by looking at the output, and by inserting a sleep 30 into "childscript.pl" so that I could see it with ps -e.
My guess here is that redirecting the output causes a new process to do the writing. But I need the pid of the program, and I have no control over the program except for the fact that I can execute it. (It needs to run in the background too.)