if(fork()) { # do some stuff in the parent as root } else { # child - run process as another user exec "/usr/bin/sudo -u some_other_id $command_to_run" }