How do I execute a command as the original user from a sudo'd program?
I had this problem with Samba on a NAS while creating files from a web-application.
In this case I retrieved the running processes and determined which user was running samba.After isolating that name, I had the correct user account. Maybe this will work for your situation as well?
In this case I imagine you need to filter through the ps output and lookup the name of your program. See if that works by testing it on bash.