With forking and using pipes, you can also set the effective UID/GID for the script before the script starts. See https://jineshkj.wordpress.com/2006/12/22/how-to-capture-stdin-stdout-and-stderr-of-child-program/