Did you try all in one shell call (using semicolons or a string of several lines) starting with sudo? Your cgi could be a 'very restricted sudoer'.