How does the scheduler find the primary group?

The discussion on this page might be helpful

Check this: getpwnam()

Hook and wrapper scripts are the way to go.